Email notification of cron job?

Can anyone please let me know how to get an email notification to say whether the cron job has run ok and emails to say when it hasn’t run ok ?

Any help appreciated.


Hello, Dez.

Take an example #1:

30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete

Crontab owner recieves non-dummy results by mail.
You can change owner’s e-mail address by environment string MAILTO.

And the second:

30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete| \\
           mail -e -s "task #1 report"

Where ‘-e’ mean don’t send zero sized messages.

Both variants sends you stdout of the tasks.
You can suppress stdout reports with redirection to /dev/null.
Stderr reports you will recieve if something wrong were on task execution.
It may be something like ‘ls: asdasd: No such file or directory’ or something else.

Add ‘2>&1’ before the ’ >/dev/null’ for stderr suppressing.

30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete  \\
           2>&1 >/dev/null && echo The task was ended at `date` | \\
           mail -e -s "task #1 report"

It sends something like

From: yourlogin
Subject: task #1 report

The task was ended at Mon Jun 16 03:30:08 MSD 2008

Good luck!


First, WELCOME to SitePoint!

Second, thanks for that excellent post!



Hi Xопа, Many thanks for taking the time to post such detail - it’s appreciated.

The terminology has me a little baffled, but that’s because I’ve only been using crons for a very short time.

One thing, is it possible to do all of that on a shared server ?


Hi Dez,

Yes. All users can use ctontabs. Except of crontab-denyed users.
Check your hosting rate (tarif?).

Do you know, how will work apache on opensolaris?
I want to make an installation on my home server.

[QUOTE=жопа;3858062]Hi Dez,

Yes. All users can use ctontabs. Except of crontab-denyed users.
Check your hosting rate (tarif?).[/QUOTE]

Many thanks - will work on this some more.

жопа, this is a trickier question than that.

Linux always emails any output of the cronjob to the owner of the job. System crons email it to the root user (unless specified otherwise). User crontabs email the user. If you only care about the outputs, all you have to do is to set a working email address for that user. How you do that is up to the mail system you run.

However, to get notifications only when the cronjob is successful, you need to do more scripting than this, because you need to determine exactly what’s considered successful and what’s not. This varies from case to case.

Sysadmins typically integrate such scripts with their monitoring platform. I personally use Nagios and for example integrate my cron backup scripts with it so that I can monitor (and get notified) when the backups fail or succeed.

What gets tricky is what I meant by the case by case basis earlier. Even within the same use, like backup, there are servers I know will get errors when running a rsync backup on them because their files get modified very regularly and it doesn’t matter so much if I have a few errors, so overall the backup is still a success. Whereas if I’m backing up database dumps and get a file error on any dump file, that’s a failure.

If you can’t be bothered scripting such things, just get the cron daemon to email you the whole output then you can manually shift through it and see for yourself.