DEV Community

Cover image for Get emailed if a cron job never ran
Gabrian Mak
Gabrian Mak

Posted on

Get emailed if a cron job never ran

A cron that never starts cannot email you. I found that out the hard way with a nightly database backup. I ended up hardcoding a ping that only fired after the dump finished. If the ping never showed up, the backup did not happen. Silent failure. No email from the job itself.

Same class of bugs: crontab removed in a deploy, the box down at 2am, a lock file that never cleared, a script that exited before it did the real work.

The pattern
Do not wait for the job to report failure. Expect a heartbeat after success. If the next one never arrives, that is the failure.

This is not uptime monitoring. You are not polling a public URL. The job tells you it ran.

Ping only when the script actually succeeded:
# Nightly job at 2:00 AM — ping only if backup.sh succeeded
0 2 * * * /usr/local/bin/backup.sh && curl -fsS -X POST https://www.gonewatch.com/api/heartbeat/YOUR_TOKEN

&& matters. A failed dump should not look healthy.

One URL per job. Database dump, file sync, and offsite copy are three different things. If you fold them into one ping, you will not know which part died.

Give yourself a grace window. A dump that runs 20 minutes late is not the same as a dump that never ran. Tune grace to how late the job can be without it being a problem.

GitHub Actions
GitHub emails a failed run. It does not email a scheduled workflow that never started. Last step, success only:
- name: Heartbeat
if: success()
run: curl -fsS -X POST "$HEARTBEAT_URL"
env:
HEARTBEAT_URL: ${{ secrets.HEARTBEAT_URL }}

What I wrapped it in
I got tired of maintaining that ping by hand, so I put it in Gonewatch. Same idea: create a monitor, paste the curl (or a tiny GitHub Action), get emailed if the ping stops. Free to start, no credit card.

Top comments (0)