How to monitor cron jobs and backups with heartbeat checks

A heartbeat check turns silence into an alert. Give the job a unique URL and call it when the job succeeds: backup.sh && curl -fsS https://hb.wardterm.com/<token>. The && sends the ping only if the job exits with 0. If no ping arrives within the expected period plus a grace time, you get an alert, whether the job failed, hung, or never started.

Updated . The crontab line and the script were run with curl 8.7 on macOS against a local test endpoint, with a job that succeeds and one that fails.

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

  1. The monitoring service gives the job a unique URL.
  2. Each time the job succeeds, it calls the URL.
  3. 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.
  • -f makes curl fail on an HTTP error, -sS hides the progress bar but keeps errors, so cron still mails them if mail is set up.
  • -m 10 stops curl after 10 seconds, so a network problem never hangs the job.
  • --retry 3 tries again after a timeout or a temporary server error.
  • Use full paths. cron's PATH is 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:

JobExpected everyGrace
Queue worker every 5 minutes5 min5 min
Hourly sync60 min15 min
Nightly backup of about 20 minutes1440 min60 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

  1. Open Monitoring and choose Add monitor.
  2. Choose the type Heartbeat (cron) and give it a name, which only you see.
  3. Set Expected every (min) and Grace (min).
  4. Save. The Ping URL appears, with the Command, the Crontab line and Report a failure, each ready to copy.
  5. Paste the line into crontab -e on the server, with your job in place of your-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.

Sources

All guides