How to monitor cron tasks and get emails when they don't run?

Solution 1:

I think monitoring syslog would be the easiest solution.

Have your syslogs forward to your monitoring system, and then set up alerts within your monitoring system.

I've also set up custom SNMP MIBs in the past, which you could put the timestamp of the last time that particular cronjob has run. Then some external system could monitor that snmp MIB for a timestamp older than 24 hours.

Solution 2:

Your solution is workable, but it reinvents some wheels that you probably don't need to.

First, you should really have some type of monitoring service. I tend to use nagios, but there's a ton out there. Choose one of those systems and have it monitor your cron daemon.

Then write a plugin that will utilize the wrappers that voretaq7 mentioned. You will have an alert if the cronjob fails and if crond also fails.

The reason I suggest this is that you'll have all your monitoring in one place. You'll eventually have to have a site wide monitoring system and it makes more sense to put your effort into that rather than having a scattered series of monitoring systems in place.

Solution 3:

I built a simple tool for this type of monitoring - https://cronitor.io

It allows you to set both intervals (every 24 hours) and durations (greater than 10 minutes, less than 2 minutes, etc), and then receive email/SMS alerts if your cron job (or any other automated task) doesn't run according to the rules you defined.

The tool is free for individual monitors and paid plans are available for those with multiple monitoring needs.

Solution 4:

Your solution sounds OK based on your environment, but it might be a bit of overkill (unless you need to be able to audit the history of this job over the long term, in which case the database bit might make sense).

Another option to consider is simply wrapping your cron jobs in a check script (if the cron job exits with an error status (!=0) send an email, or generate output and let cron send the email for you).