Why cron jobs fail without a sound
cron starts a job and forgets it. If the job fails, the error goes to a local mailbox nobody reads, or nowhere. If it never starts, nothing at all is written.
Common causes:
- A full disk, an expired password or a moved file makes the script fail.
- The crontab was lost when the server was rebuilt, or the job was commented out during a fix.
- cron runs with a short
PATH, so a command that works in your shell is not found. - The job hangs and never finishes, so the next run piles up behind it.
Logs and error emails only help when something runs. To catch a job that did not run, turn it around: the job checks in, and silence is the alarm.
How a heartbeat check works
- The monitoring service gives the job a unique URL.
- Each time the job succeeds, it calls the URL.
- The service knows how often to expect the call. If none arrives within that period plus a grace time, it alerts you.
This is also called a dead man's switch. It catches every case above, including the server being off, because a missing ping looks the same whatever the cause.
Add the ping to your crontab
Open the crontab with crontab -e and put the call after the job:
0 3 * * * /usr/local/bin/backup.sh && curl -fsS -m 10 --retry 3 https://hb.wardterm.com/<token> > /dev/null
&&runs curl only if the job exits with 0. Use it rather than;, which pings even after a failure.-fmakes curl fail on an HTTP error,-sShides the progress bar but keeps errors, so cron still mails them if mail is set up.-m 10stops curl after 10 seconds, so a network problem never hangs the job.--retry 3tries again after a timeout or a temporary server error.- Use full paths. cron's
PATHis usually only/usr/bin:/bin.
To be told at once when the job fails, instead of when the grace time runs out, call the URL with /fail at the end:
0 3 * * * /usr/local/bin/backup.sh && curl -fsS -m 10 --retry 3 https://hb.wardterm.com/<token> || curl -fsS -m 10 https://hb.wardterm.com/<token>/fail
The part after || runs when the job fails, and also when the first ping cannot get through. Run the same line once in a shell to check that the ping arrives.
Jobs with several steps
For a backup that dumps, compresses and copies, a short script keeps the crontab readable and reports the result of the whole job:
#!/bin/sh
URL='https://hb.wardterm.com/<token>'
if /usr/local/bin/backup.sh; then
curl -fsS -m 10 --retry 3 "$URL" > /dev/null
else
curl -fsS -m 10 --retry 3 "$URL/fail" > /dev/null
fi
Put set -e at the top of backup.sh so it stops and exits with an error at the first step that fails. Without it, the script's exit code is only that of its last command.
A job can also succeed and produce nothing useful. Ping only if a recent, non-empty backup file exists:
find /var/backups -name 'db-*.sql.gz' -mmin -90 -size +0 | grep -q . && curl -fsS -m 10 --retry 3 https://hb.wardterm.com/<token> > /dev/null
For backups over SSH, see rsync over SSH: rsync exits with an error code that && picks up.
Choose the period and the grace time
The period is how often the job runs. The grace time is how late it may be before you are alerted. Set the grace longer than the job's normal running time:
| Job | Expected every | Grace |
|---|---|---|
| Queue worker every 5 minutes | 5 min | 5 min |
| Hourly sync | 60 min | 15 min |
| Nightly backup of about 20 minutes | 1440 min | 60 min |
Too short a grace gives alerts on a slow night. Too long, and you hear about a missing backup late.
Set it up in Wardterm
- Open Monitoring and choose Add monitor.
- Choose the type Heartbeat (cron) and give it a name, which only you see.
- Set Expected every (min) and Grace (min).
- Save. The Ping URL appears, with the Command, the Crontab line and Report a failure, each ready to copy.
- Paste the line into
crontab -eon the server, with your job in place ofyour-job.sh.
The URL accepts GET, POST and HEAD. You get one alert when the job is late and one when it checks in again, never one for each missed run. To check the servers themselves, add an uptime check.
Questions
How do I know if a cron job failed?
Make the job call a heartbeat URL when it succeeds. A monitoring service that expects the call at each run alerts you when it does not arrive in time.
What is a dead man's switch for cron?
Another name for a heartbeat check: the job proves it ran by checking in, and silence counts as a failure. It catches jobs that never started, which a log or an error email cannot.
What grace period should I set?
Longer than the job's normal running time plus some delay. For a nightly backup that takes 20 minutes, a grace of 60 minutes avoids alerts on a slow night.
Why use && instead of ; before curl?
With && the ping runs only if the job exits with code 0. With ; it runs every time, so a failed job would still look healthy.