Notifications for failed cron jobs


Every few years when I setup a new computer I stumble over the same question: "How do I get notified of failed cron jobs again?". It is pretty simple, but as I tripped over it again today, here is a little write-up so I do not have to google this again. Or potentially I will and then hopefully this post will turn up in the search results.😉

As cron is an old program from the days where the server use case was the predominant one, it will send emails with the output it's jobs will produce. Now I don't want my desktop machine to send emails over the internet to myself about broken jobs, when I am working on the very same machine at the same time.

So I set the MAILTO variable to my username. This is possible because apparently in the old times people liked to send messages to people living on the same server. Now we just need some means to receive that mail.

On Ubuntu (and I guess also on Debian) we can get that easily by installing the bsd-mailix package and choosing localhost in the package configuration. In you shell you will then shortly read "You have new mail" if any cronjobs are running. You can read that mail by running mail. To get a list of the possible commands just type a question mark.

Obviously having every single cronjob run produce an email is not useful. Only failed job runs should lead to an email. This is where chronic comes in. Just prepend any cronjob with it and all output is suppressed unless the job fails. It is part of the moreutils package.

That's it. Happy scheduling!

cron  linux 

See also