Last Updated: February 25, 2016
·
22.52K
· alup

Get email when a cron job fails

To get an email every time a cron job, you have scheduled to run, fails:

  1. Redirect all of your standard output to /dev/null or to some file.

  2. Use the MAILTO environment variable with a list (comma separated) of all the email addresses you want to receive the email notifications.

To avoid using the same value of MAILTO for every task you run, you can just write it in the crontab e.g.:

MAILTO="foo@bar.com"
0 5 * * * /bin/some_script > /dev/null

MAILTO="foo@bar.com" to the top of a crontab will cause any output from the cron job to be emailed. So we redirect the STDOUT to null and if any STDERR messages are present, they will get emailed to you.