A cron job that fails is annoying. A cron job that hangs is worse: it never exits, so nothing that waits for an exit code ever fires. No error mail, no || notify, no red line in your monitoring. Meanwhile the next run starts on top of it, and the one after that.
A cron job timeout is the first fix. This post covers three layers, from zero-install to a small wrapper:
- put a hard time limit on the job with GNU
timeout - stop runs from piling up with
flock - get an alert that says it hung, not just it failed
Everything below was run on Debian-style Linux with GNU coreutils 9.7 and util-linux 2.41. These examples assume GNU timeout from coreutils (timeout --version). BusyBox and some other implementations exist; their exit codes and child-process behavior can differ, so check the manual on your box.
1. Set a cron job timeout with GNU timeout
GNU timeout is already on almost every Linux box. It runs a command and sends it SIGTERM when the time is up:
# kill the backup if it is still running after 2 hours 0 3 * * * timeout 2h /opt/scripts/backup.sh Things worth knowing:
- Exit code 124 means "timed out". Anything else is the command's own exit status. That makes a hang distinguishable from a normal failure in a script:
timeout 2h /opt/scripts/backup.sh rc=$? [ "$rc" -eq 124 ] && echo "backup hung" Child processes are included. By default
timeoutputs the command in its own process group and signals the whole group, so a shell script that started a backgroundrsyncdoesn't leave it running. (--foregroundturns that off. You don't want it in cron.)Some jobs ignore
SIGTERM. Add-kto follow up withSIGKILL:
0 3 * * * timeout -k 1m 2h /opt/scripts/backup.sh If the KILL is needed, the exit status is 137 (128+9), not 124. The GNU manual lists all the codes.
2. Stop overlapping cron runs with flock
A timeout caps one run. It doesn't stop cron from launching the next run while the current one is still inside its limit, for example a job every 5 minutes with a 15-minute timeout. flock takes a lock file and, with -n, gives up immediately if the lock is held:
*/5 * * * * flock -n /run/lock/sync.lock timeout -k 30s 15m /opt/scripts/sync.sh When the previous run still holds the lock, flock -n exits 1 without running anything. Because the command itself can also exit 1, -E lets you pick a distinct code for "skipped because locked":
flock -n -E 75 /run/lock/sync.lock timeout 15m /opt/scripts/sync.sh Order matters a little: with flock … timeout … the lock is held for the whole run, including the kill. See flock(1).
3. Get an alert when the cron job hangs
timeout and flock keep the box healthy, but they are silent. Cron mails a job's output after it finishes, if mail is even set up, and nothing tells you that run number 3 was killed after two hours.
You can write that check in shell (capture the exit code, test for 124, curl a webhook). I got tired of copying that block between servers, so I wrote a small wrapper for it. Full disclosure: I'm the author. It's called cronpulse-lite: one Python file, standard library only (3.9+), MIT.
pip install cronpulse-lite # or: pipx install cronpulse-lite export CRONPULSE_LITE_WEBHOOK=https://hooks.slack.com/services/XXX/YYY/ZZZ # or Discord, or any URL Prefer no pip? It's one file, so you can just download it:
curl -fsSLO https://raw.githubusercontent.com/alessio1999xx-debug/cronpulse-lite/main/cronpulse_lite.py chmod +x cronpulse_lite.py Cron runs with a minimal PATH, so use the full path to the command (which cronpulse-lite tells you where pip put it):
0 3 * * * flock -n /run/lock/backup.lock /usr/local/bin/cronpulse-lite run --name db-backup --timeout 2h -- /opt/scripts/backup.sh What it does on each run:
| Situation | Result |
|---|---|
| job exits non-zero | logged as failed, alert sent, same exit code returned |
job runs past --timeout | process group gets SIGTERM, then SIGKILL after 5 s if it is still alive; logged as timeout, alert sent, exit 124 |
| job succeeds | logged as ok, no alert |
A test with a job that ignores SIGTERM (so the second step is needed):
$ ./cronpulse_lite.py --log ./cp.jsonl run --name stubborn --timeout 1 -- sh -c 'trap "" TERM; sleep 8' cronpulse-lite: stubborn timeout (exit -9, 6.003s) $ echo $? 124 The alert is Slack text or Discord content. Any other URL gets the JSON run record (job, status, exit_code, duration, output, host). Runs go to a JSONL file you can grep or jq, and cronpulse-lite status exits 1 if any job's last run failed, so another check can read it.
What none of this catches
All three layers need the job to start. If the server was off at 03:00, crond was stopped, or someone deleted the crontab line, there is no process to time out and nothing reports anything. Silence looks exactly like success.
That gap needs the opposite approach: something outside the job that expects a check-in every N hours and alerts when it doesn't arrive (a "dead man's switch"). Hosted services like Healthchecks.io do this. I also sell a self-hosted version, CronPulse CLI: same run wrapper plus add --every 1d for missed check-ins, a --max-runtime overrun alert that doesn't kill the job, peak-memory tracking, Telegram alerts, and a small dashboard. It's here if that's the part you need. For timeouts and hang alerts alone, the free script above is enough.
Free repo: https://github.com/alessio1999xx-debug/cronpulse-lite
How do you handle cron jobs that hang: timeout everywhere, systemd timers with RuntimeMaxSec=, or something else?
