How to monitor a cron job, and know when a backup stops running
Last checked
In short
A scheduled job that stops running produces no error, because nothing runs to produce one. The way to notice is to turn the check around: the job tells a monitoring service each time it finishes, and the service alerts you when it has not heard from the job in time. That is a heartbeat, also called a dead man's switch.
It takes one extra line in the job: a request to a URL after the command succeeds.
Why cron jobs fail without anyone noticing
- The server was rebuilt or replaced, and the crontab was not copied.
- The disk filled up, and the job exits at once every night.
- A password, token or certificate the job uses expired.
- The job hangs on a lock or a network call, so the next runs never start.
- Cron's own email about the failure goes to a local mailbox nobody reads.
Add a heartbeat
Create a heartbeat monitor and you get a URL that belongs to this job alone. Call it when the job succeeds:
0 2 * * * /usr/local/bin/backup.sh && curl -fsS --retry 3 https://hooks.raqibo.com/ping/your-tokenThe && matters: the request is made only if the script exits successfully. If the script fails, nothing is sent, the deadline passes, and you are told. The curl options keep it quiet (-sS), make HTTP errors count as failures (-f) and retry a network blip (--retry 3).
Report failures at once
Waiting for a missed deadline is fine for a job that never started. When a job runs and knows it failed, it can say so straight away by calling the same URL ending in /fail:
0 2 * * * backup.sh && curl -fsS --retry 3 https://hooks.raqibo.com/ping/your-token || curl -fsS --retry 3 https://hooks.raqibo.com/ping/your-token/failMake the script's own checks strict so that failure is detected: exit non-zero when the backup file is empty or smaller than yesterday's, not only when a command crashes.
Choose the period and the grace time
- Period: how often the job runs. For a job that skips some days, use the longest gap between runs - a weekday-only job has a three-day gap over the weekend. Otherwise every Monday is a false alarm.
- Grace: how late a run may be before you are told. Cover the job's longest normal run and add some slack. A nightly backup that takes 20 to 40 minutes wants about an hour.
- Too tight, and a slow night wakes someone for nothing, after which the alert gets muted. Too loose, and a real failure waits a day. Start loose and tighten once you have seen a week of real run times.
Outside cron
- Windows Task Scheduler: add a final action that runs PowerShell with Invoke-WebRequest to the URL, or call it at the end of the script.
- Kubernetes CronJob: add the curl to the end of the container's command, joined with &&.
- GitHub Actions on a schedule: add a last step that runs curl, with if: success().
- systemd timers: add an ExecStartPost= line with the curl to the service unit.
- Inside a program: make an HTTP GET to the URL as the last thing a successful run does.
Things to get right
- One heartbeat per job. Sharing one between two jobs hides the failure of either.
- Treat the URL as a secret: anyone who has it can report the job alive. Replace it if it leaks.
- Send alerts somewhere a person looks. A heartbeat that emails the same unread mailbox as cron has solved nothing.
- Test it: comment the job out for one period and confirm that the alert arrives.