I have a cronjob scheduled to run everyday. How can I obtain an email if and only if the cronjob fails?
I am using Sun Solaris (ksh).
:rolleyes:
I have a cronjob scheduled to run everyday. How can I obtain an email if and only if the cronjob fails?
I am using Sun Solaris (ksh).
:rolleyes:
You could write a wrapper script to run your job...
Make sure, though, to use full path names in both your cron job script, and the wrapper.
For example, if your script, let's call it fullback.ksh, makes a full backup of your /home filesystem, you could write a script like this (let's call this wrap-full.ksh):
#!/usr/bin/ksh
echo "\nStart : `/bin/date`" >> /var/adm/backup_log
/home/admin/bin/fullback.ksh >/var/adm/backup_log 2>&1
if [[ $? -ne 0 ]]; then
/bin/mail admin <<EndOfMail
Subject: Cronjob error
It looks like there was a problem with your cronjob.
Check the log at /var/adm/backup_log for details.
.
EndOfMail
fi
echo "Finish : `/bin/date`" >> /var/adm/backup_log
exit 0
This assumes, of course that your backup script will pass the return code out to it's controlling process; in this case, the wrapper script.
It should be said though, that this should be tested first, since I do not use this type of reporting myself, and I haven't tested this approach.
Hope it helps though.
Thanks LivinFree for the script.
What happens to the script you have written, if there is no exit code? The script I am calling will provide me exit code only if their is some error and else no exit code is provided. Will the script work if no exit code is obtained (per your script, say, fullback.sh provides exit code only if there is some error)?
What I meant by exit code is the value of $?.
Under normal circumstances, like a successfully run command, the exit code will be 0. If there's an error, it will be something else. The sample script above checks to make sure that $? = 0, and if not, it will send an email.
The concern I have is that something in whatever script is run may run successfully after the failed command... I don't know if I'm explaining very well, so I'll try to show you:
$ cat tmp1.sh
#!/bin/sh
echo
wuzzle
$ ./tmp1.sh
./tmp1.sh: wuzzle: command not found
$ echo $?
127
$ cat tmp2.sh
#!/bin/sh
wuzzle
echo
$ ./tmp2.sh
./tmp2.sh: wuzzle: command not found
$ echo $?
0
So, you see, unless you told the fullback script to "exit 1" (or exit anything not equal to 0) after any component failed, it might not email you if it does fail.
It depends completely on your script (the fullback.ksh one), is should be easy to make sure it does this correctly...
On the other hand, if you're going to be rewriting to do this, why not put them all together into one? Here's a very simple example:
#!/usr/bin/ksh
/bin/tar vcf /dev/rmt/0 /home/admin >/var/adm/backup_log 2>&1
if [[ $? -ne 0 ]]; then
/bin/mail admin <<EndOfMail
Subject: Your backup had errors
See the contents of /var/adm/backup_log
for more details.
.
EndOfMail
fi
I guess it all depends on what you're trying to do.
And please let me know if I don't make any sense... I'm kind of tired here 
Thanks LivinFree for the details. I understand what you are saying.
I am trying to stop/start a server (i.e, bouncing the server) through a cron job using the stop/start script provided in the server. The script returns exit value only if there is an error. (i.e, if (error) then exit 2). If there are no error, i.e, if the server stop/start without any problem, then there is no exit code. So, when there is an clean start, since exit value is nothing, what will [$? -ne 0 ] return? Will it return true or false?
Hope I have explained my problem properly.
If the script does "exit 2" when it fails, you will get an email using the same logic as the script above.
If the script completes normally, then it should exit 0 and give you no email.
Did I answer your question?
yep..Thanks for the information and the script.
I tested it and it works fine.

If all you are looking for is email from a failed cron job, then using the redirect would be simplier.
Two cron jobs - one has an error in the entry - this would be the same as a script running and sending back an error. The first never sends output to you because it doesn't error (normally). The second sends email because the syntax is wrong.
$ crontab -l
Your "cron" job on medusa
date '+
produced the following output:
? q