[problem]
You want to receive an email upon cron job failure.
[/problem]
[solution]
The example code can be wrapped around a normal cronjob, to send an email if the cron fails.
[/solution]
[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 )'
Example of generating an email upon cron failure
[/example]
[reference]
[tags]bash, cron email, Unix Coding School[/tags]
- Linux Man Pages – crontab command
- Linux Man Pages – bash command
- Linux Man Pages – mail command
- Linux Man Pages – uname command
- Linux Man Pages – date command
[/reference]
If you have found my website useful, please consider buying me a coffee below 😉