Uptimecat

Cron job heartbeat monitoring in practice

Scheduled jobs fail quietly. A backup that stopped three weeks ago looks exactly like one that runs every night, until you need it.

Why jobs fail silently

Cron runs a command and moves on. If the command fails, cron at best sends a local mail that nobody reads. If the server is off, the clock is wrong or the crontab was lost in a rebuild, nothing runs and nothing complains. Silence looks the same as success.

An outside check cannot help here, because there is nothing to connect to. The job has no port and no URL. The fix is to reverse the direction: the job reports in, and the monitor raises an alert when a report is missing.

How a heartbeat monitor works

In Uptimecat this is a Push monitor. It gives you a unique URL. Your job calls that URL when it finishes. The monitor expects a call within the interval you set. If none arrives, it goes down and your alert channels fire. The step by step setup is in heartbeat monitoring.

Place the call after the work

#!/bin/sh
set -e
out=/backups/mydb-$(date +%F).dump
# the real work: any failing command stops the script here
pg_dump -Fc -f "$out" mydb
test -s "$out"
# only reached when everything above succeeded
curl -fsS -m 10 "https://dashboard.uptimecat.co/api/push/YOUR_PUSH_TOKEN?status=up&msg=backup%20ok"

The order is the whole trick. Send the heartbeat as the last step, and only when the work succeeded. A heartbeat sent at the start of a job only proves the job started. A heartbeat sent from a separate timer proves nothing about the job at all.

Check the output too, not just the exit code. The example tests that the backup file is not empty. A job that exits cleanly but writes nothing is still a failure. Avoid pipes such as pg_dump | gzip in these scripts: a plain sh script only sees the exit code of the last command in a pipe, so a failed dump can still look like success.

Sizing the interval

ScheduleSuggested intervalReason
Every 5 minutes10 to 15 minutesAllows one slow or skipped run.
Hourly70 to 90 minutesCovers normal variation in run time.
Daily25 hoursOne hour of margin for slow runs and clock drift.
Weekly7 days plus a few hoursLong margin, since a missed run is rarer but costly.

Report failures as well

Waiting for silence works, but it is slow for a daily job. If your script can detect its own failure, report it at once with status=down and a short message. You learn in seconds instead of a day, and the message tells the responder what broke.

Use one monitor per job. Separate monitors tell you exactly which job stopped and let each have its own interval. Name them after the job, such as nightly-db-backup, so the alert is clear without opening the dashboard.

Frequently asked questions

What if the job runs only on weekdays?

Set the interval to cover the weekend gap, or pause the monitor with a recurring maintenance window.

Is the push URL a secret?

Yes. Anyone with it can send a heartbeat. Keep it in the job's configuration, not in a public repository.

Can I build cron schedules here?

Use the cron expression generator to write and check a schedule before you size the interval.

Related

Open the dashboard

Sign in with your Google account to add a monitor and pick an alert channel. Access is currently limited to Moruk accounts; open sign-up is not available yet.

Sign in