The backup that quietly stopped running: why cron jobs need heartbeats

Cron jobs fail without telling anyone. Here's why that happens, how to catch it with a simple heartbeat ping, and the exact crontab lines to copy.

AskMyServers team · · 3 min read

Here's a story most of us have lived through in some form. A nightly database backup was set up months ago. It worked. Everyone stopped thinking about it. Then one day you need to restore something, open the backup folder, and the newest file is eleven weeks old.

Nothing alerted anyone, because nothing failed in a way that makes noise. The job simply stopped running.

Why cron jobs fail silently

Cron is wonderfully simple, and that simplicity is the problem. It runs a command at a time. If the command exits with an error, cron might try to email the output to a local mailbox nobody reads. If the command never starts at all, nothing happens anywhere.

Some of the usual causes:

  • The server was rebuilt and the crontab wasn't copied over.
  • A password or API key used by the script changed.
  • The disk filled up, so the dump started and then died.
  • Someone commented the line out "temporarily" while debugging.
  • The script now takes longer than the gap between runs and overlaps itself.

Monitoring CPU and memory won't catch any of these. The server is perfectly healthy. It's just not doing the job you think it's doing.

Flip the question around

Instead of watching for failure, watch for success. The idea is called a heartbeat (or a dead man's switch): the job pings a URL every time it finishes. If the ping doesn't arrive on schedule, something is wrong, whatever the reason.

That last part is the nice thing about it. You don't have to predict how the job will break. Missing ping means investigate.

The crontab line

In AskMyServers you create a heartbeat under Cron jobs, tell it how often the job should run, and get a private ping URL. Then you change your crontab from this:

0 2 * * * /opt/scripts/backup.sh

to this:

0 2 * * * /opt/scripts/backup.sh && curl -fsS -m 10 https://askmyservers.com/api/v1/ping/YOUR_TOKEN > /dev/null || curl -fsS -m 10 https://askmyservers.com/api/v1/ping/YOUR_TOKEN/fail > /dev/null

If the script succeeds, it pings the normal URL. If it fails, it pings /fail so you hear about it immediately, rather than waiting for the deadline to pass. The -m 10 keeps a slow network from hanging your job.

A slightly better backup script

If your script does several things, make it stop at the first failure and report what happened:

#!/usr/bin/env bash
set -euo pipefail

PING="https://askmyservers.com/api/v1/ping/YOUR_TOKEN"
trap 'curl -fsS -m 10 --data "failed at line $LINENO" "$PING/fail" > /dev/null' ERR

pg_dump -Fc mydb > "/backups/mydb-$(date +%F).dump"
find /backups -name 'mydb-*.dump' -mtime +14 -delete

curl -fsS -m 10 "$PING" > /dev/null

The text you send with --data shows up next to the ping, which saves a trip to the server to find out which step broke.

Picking the grace period

Jobs don't always finish at the same minute. A backup that takes ten minutes on a quiet night might take forty on a busy one. Set the grace period to cover your slowest normal run, not your average one. Too tight and you'll get false alarms; too loose and you'll learn about a missed backup half a day late.

What to put a heartbeat on

Anything you'd be upset to discover had stopped:

  • Database and file backups
  • Certificate renewals, if you run them yourself
  • Report or invoice generation
  • Data syncs and cleanup jobs
  • Queue workers that are supposed to check in regularly

It takes two minutes per job, and it turns "I think the backups run" into "I know they ran last night".

Set up your first heartbeat

The backup that quietly stopped running: why cron jobs need heartbeats · AskMyServers