published on 19 March 2023
Crontab mail notifications are great, except when they're spamming your inbox, even though the command ran fine and without issues. One might be tempted to do something like this to stop the madness:
MAILTOemail@example.com 0 15 * * * /run/this/script.sh > /dev/null 2>&1
But then you won't get any notifications and that's not really an option if
things fail (at least for me). What I do is use
chronic (on Ubuntu it's in
to execute my commands:
MAILTOfirstname.lastname@example.org 0 15 * * * chronic /run/this/script.sh
That way, cron only sends a mail if the command did not run successfully (i.e. if the return code isn't zero) and discards the command output on stdout otherwise, thus freeing your inbox from cron mail spam. The great thing is that you can set it individually for each job, so if you wanted to be sure that a command ran, you can just leave out chronic and the output redirection and still get that confirmation mail.
Apparently there is also a utility called cronic (without the 'h') which supposedly does the same and was written by the same author while also being newer, but in my testing it still sent mail when a command ran successfully. Your mileage may vary, but chronic just works for me at the moment.
By the way, crontab.guru is super helpful when devising more complex cron schedules.