Problem
You want to receive an email upon cron job failure.
Solution
The example code can be wrapped around a normal cronjob, to send an email if the cron fails.
Example
Add to beginning:
bash -c '( msg=`
Add to the end:
2>&1 > /tmp/logfail.log`; if [ ! -z "$msg" ] ; then echo "$msg" |
/bin/mail -s "`/bin/uname -n`:$LOGNAME:`/bin/date`: YOUR MESSAGE" YOUR_MAIL_LIST; fi )'
Demo
# # # # # bash -c '( msg=`YOUR COMMAND 2>&1 > /dev/null`; if [ ! -z "$msg" ] ; then echo "$msg" |/bin/mail -s "`/bin/uname -n`:$LOGNAME:`/bin/date`: YOUR MESSAGE" YOUR_MAIL_LIST; fi )'
Reference
[tags]bash, cron email, Unix Coding School[/tags]