Cronjob for reboot not working - linux

I am trying crontab but crontab for email is not working.However, I want that crontab runs a shell script and that shell script runs a python script.
One crontab I tried is
* * * * * echo " This is current date and time $(date)"
but this is not getting printed on screen. I don't understand what is wrong I am doing.

for sending the email form this command.
echo " This is a message " | mailx -s "Subject" mymail#email.com
you need to make sure port 25 is open on your system.
you can use the mailq command for check email in the queue on your system.
you can check local port is open or not from
telnet localhost 25
nc localhost 25

Related

Crontab not giving results

I am not in the root,
I entered the following commands in the crontab:
*/1 * * * * /home/ajain/testscript.sh
The file testscript.sh has the following commands:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date` >> /home/ajain/testscript.log
echo "The script ends now"
exit
The crontab is not giving the results, however, the following command is giving the result in the testscript.log file correctly, displaying the ping date.
bash testscript.sh
Why is the crontab not working?
You can fix it in two different ways.
To provide full path to the script /home/ajain/testscript.sh. Here you don't even need to add bash because you have clearly mentioned in which shell your script should run i.e. first line of your script #!/bin/bash
Add this line before executing the script
set path=$path:/home/ajain/
testscript.sh # no need to use bash in front of it
Also providing execution permission to a script is not just enough. You need to check whether the user who is going to execute the script has permission to the location of the script or not. That means whether user can do a cd /home/ajain/ or not.
Hope this will help you.
remove >> /home/ajain/script.log, just add line to the top of file /home/ajain/testscript.sh:
#!/bin/bash
exec >> /home/ajain/script.log 2>&1
echo "The script begins now"
ping -c 2 live.com
echo "The script has been run on `date`"
echo "The script ends now"
exit
remove >> /home/ajain/script.log from crontab, just using:
*/1 * * * * /home/ajain/testscript.sh
You need do define script output.
Because of cron man page:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in
the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen
in the syslog and ps output.
Fore everything else than echo The script has been run on 'date' >> /home/ajain/testscript.log you should check your/root's mail, or the syslog (eg. /var/log/syslog).
I could recommend make something like this:
*/1 * * * * /home/ajain/testscript.sh >> /home/ajain/script.log
#Edit
My crontab file on my user:
$crontab -l
# test
*/1 * * * * /home/3sky/tests/test.sh >> /home/3sky/tests/log.log
Script look like that:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date`
echo "The script ends now"
exit
Permission on files:
test.sh - 0755/-rwxr-xr-x
log.log - 0644/-rw-r--r--
Output in log file:
$tail -20 log.log
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:18:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:19:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:20:12 CEST 2018
The script ends now

How to mail the path cron.log file?

I am using a crontab to run my scripts periodically, Here is the code that I have added in crontab -e
* * * * * /usr/bin/python3 /home/mark/WORKSPACE/ep_prac/scripts/main.py > $HOME/test-cron.log 2>&1; mail -s "CronJob is run successfully" abc#gmail.com, xyz#gmail.com < /home/mark/test-cron.log
I want to mail the path of "test-cron.log" file. I am running crunch of time, searched a lot but couldnt file a relevant solution
Instead of sending the path, I found a way to attach the log file itself and mail it to user.
mail -A "$HOME/test-cron.log -s "Cron Job run success" xyz#gmail.com"

How to read grep command result in linux?

I want to monitor jboss server status in linux for keep up & running the application.My requirement is if server down , i need to get the email from server.
So i'm planning to run crontab in linux for every hour or half an hour to check the server status.
30 * * * * ps -ef|grep java | mail test#domain.com
I' will get grep command result when running above command. So How to read the grep command result to find server is up & running ?
Please advise if anybody have alternative.
In your crontab file write this:
MAILTO="test#domain.com"
30 * * * * pgrep -lf java
At the beginning of cron add
MAILTO="test#domain.com"
This would send the output of commands in cron. You dont require to pipe the output to mail again. (reference)

cron job not mailing output of urlwatch

I have one cron job like this:
* * * * * urlwatch | mail -s "job changes" pc_xxx#msn.com
It mails every minute as expected. However when I alter the test html page on my local server it doesn't email the differences, just continues to mail a blank mail with the title 'job changes'.
When I paste the job to a prompt:
pc#dellbox:/$urlwatch | mail -s "job changes" pc_xxx#msn.com
and run before/after changing the html, it emails the differences in the 2nd email as expected.
(pc is the owner of urls.txt and the cronjob was created by pc via crontab -e)
Why does the cron version not email the urlwatch output?
This is driving me batty...
Any/all help gratefully received.
ps couldn't create urlwatch as a new tag - need 1500 rep :(
Update:
If I split the command into two bits like this:
urlwatch > ~/.urlwatch/output.txt
mail -s "output" pc_xxx#msn.com < ~/.urlwatch/output.txt
This works.
If I join the two statements with a pipe like this:
urlwatch > ~/.urlwatch/output.txt | mail -s "output" pc_xxx#msn.com < ~/.urlwatch/output.txt
I get a prompt IMMEDIATELY that says
Null message body; hope that's ok
I notice urlwatch takes 2 - 3 seconds to complete, and I understand shell commands wait for preceding commands to finish (unless you're using &?). Dunno if this is significant.
Also, I'm using sSMTP...
Use full path when calling urlwatch:
$ which urlwatch
/usr/bin/urlwatch
Then your cron have to be::
* * * * * /usr/bin/urlwatch | mail -s "job changes" pc_xxx#msn.com
Put .urlwatch directories under /root/ as the program will look there to store its data
#askchipbug: your case looks very specific, but here are the things to do that may help:
-check /var/log/mail.err or /var/log/mail.log to find hints
-crontab user/permission
-some server strips content for not a good reason
-i use mailx it works fine for me and here is my cron job in crontab -e:
* * * * * urlwatch --urls=/var/www/html/urls.txt --hooks=/home/foo/hooks.py | mailx -v -r "bar#gmail.com" -s "This is the subject line" -S smtp="smtp.gmail.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="foo2#gmail.com" -S smtp-auth-password="bardaf16charsfoo" -S ssl-verify=ignore receipient#hotmail.com
i found the info for heirloom-mailx client installation from here: http://www.binarytides.com/linux-mail-with-smtp/
hope it helps.

Running bash script in cron

I have the following bash script:
!/bin/bash
# script to send simple email
#Wait 5 seconds
#sleep 05
# email subject
SUBJECT="SUBJECT"
# Email To ?
EMAIL="EMAIL"
# Email text/message
MYIP=$(curl http://ipecho.net/plain)
LASTIP=$(head -n 1 /home/ubuntu/myip.txt)
echo "Last IP:" $LASTIP
echo "Current IP:" $MYIP
#Check if IPs are the same
if [[ "$MYIP" == "$LASTIP" ]]; then
echo "IP hasn't changed. Do nothing."
else
sendemail -f $EMAIL -t $EMAIL -u $SUBJECT -m $MYIP -s smtp.gmail.com -o tls=yes -xu username -xp password
echo $MYIP > myip.txt
fi
When I try to run it in the command line, it works perfectly. The problem starts when I include it in "crontab -e" like this: "* * * * * /home/ubuntu/myip.sh".
Then it does not work. Seem to be that the sendmail is not functioning properly.
When I do a: tail -f /var/log/syslog
Sep 18 21:48:02 gpuserver sendmail[18665]: r8J1m1gO018665: from=ubuntu, size=314, class=0, nrcpts=1, msgid=<201309190148.r8J1m1gO018665#gpuserver>, relay=ubuntu#localhost
Sep 18 21:48:02 gpuserver sendmail[18665]: r8J1m1gO018665: to=ubuntu, ctladdr=ubuntu (1000/1000), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=30314, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1]
Any ideas?
When you run a script from the command line you are utilizing your current environment. Cron doesn't have that info ... So what you should do from the command line is:
env > me
Then edit the script and include ./me at the start of the script. That way your cron task will run with the same environment.
When a cron prints something to stdout or stderr, cron itself tries to send a report by e-mail. Since you probably don't have system-wide e-mail configured, this is probably where the error message in syslog is coming from.
To prevent cron from sending any mail, make sure your script and any commands it spawns don't output anything to stdout or stderr. An easy way to do this is to add "&>/dev/null" to your cron line.
You can also add 'MAILTO=""' at the beginning of your crontab.
This all documented in the man pages for cron/crontab (man -k cron).

Resources