How to detect a job that started and never finished

Updated 5 min read

On this page
  1. Layer 1: timeout turns a hang into a failure
  2. Layer 2: a start ping and a "Stuck after" time
  3. Choosing the Stuck after time
  4. Long but healthy runs: send progress
  5. Overlapping runs: flock
  6. Test it
  7. FAQ
  8. Start with the job that piles up

To detect a job that started and never finished, do two things: wrap the command in timeout so a hang becomes a failure with exit code 124, and send a start ping before the job runs so an outside monitor can alert when no finish arrives within a set time. The first layer turns a hang into an error your scripts can see. The second still works when the server, the wrapper or timeout itself is the thing that's stuck.

A job that fails is easy to catch: it exits with an error, and something can react to that. A job that hangs never exits. It sits on a network call with no timeout, waits for a lock, or loops forever, and nothing complains. You usually find out when the next run piles up behind it, or when someone asks why the report is three days old.

Catching a hang takes two layers: one on the server that turns a hang into a failure, and one outside that notices a run that started and never reported back.

Layer 1: timeout turns a hang into a failure

GNU timeout runs a command and stops it if it's still going after a set time. When it has to stop it, it exits with code 124:

Code
timeout 30m /opt/jobs/sync.sh
echo $?   # 124 if sync.sh was still running after 30 minutes

Now a hang is just a failure with a recognisable exit code, and whatever handles failures sees it. Pick a limit comfortably above the slowest normal run.

timeout sends SIGTERM first. If the job ignores that, add -k 1m to send SIGKILL a minute later.

Layer 2: a start ping and a "Stuck after" time

timeout only works if the server is healthy enough to run it, and only after the limit has passed. An outside check can tell you while the job is still stuck, and it doesn't depend on the box.

With Stallbell, the job sends /start when it begins and its exit code when it ends, both with the same run_id. If a run has started but no finish or progress has arrived within the monitor's Stuck after time, Stallbell opens a Stuck run alert. When the run finishes, the alert closes.

Shell
#!/bin/sh
# stuck-safe: run a command with a time limit and report start and finish.
# Usage: stuck-safe PING_URL LIMIT command [args...]   e.g. stuck-safe URL 30m /opt/jobs/sync.sh
url="$1"; limit="$2"; shift 2
run_id="$(date +%s)-$$"

curl -fsS -m 10 --retry 3 -o /dev/null "$url/start?run_id=$run_id"
timeout -k 1m "$limit" "$@"
code=$?
curl -fsS -m 10 --retry 3 -o /dev/null "$url/$code?run_id=$run_id"
exit "$code"

In the crontab:

Code
*/15 * * * * /usr/local/bin/stuck-safe https://stallbell.com/p/TOKEN 12m /opt/jobs/sync.sh

If sync.sh hangs, you hear about it twice, in order: Stuck run once it passes the Stuck after time, then Failed run with exit code 124 when timeout stops it.

Choosing the Stuck after time

The default is 1 hour, and you can set anything from 1 minute to 24 hours. A good value is a little above the slowest normal run:

  1. Look at the durations on the monitor page after a week of runs. The wrapper above lets Stallbell work out each run's duration from the start and finish times.
  2. Take the longest normal one and add a margin, say 50%.
  3. Set the timeout limit above that, so the outside alert comes first.

For the example above: runs take 4 to 6 minutes, so Stuck after is 9 minutes and timeout is 12 minutes, inside the 15-minute schedule.

Long but healthy runs: send progress

Some jobs are slow on purpose: a big import, a model working through a long queue. Instead of raising Stuck after to hours, send a /progress ping with the same run_id as work moves along. Each progress ping resets the Stuck after clock for that run.

Code
curl -fsS -m 10 -o /dev/null "https://stallbell.com/p/TOKEN/progress?run_id=$run_id"

A run that stops sending progress for longer than Stuck after is flagged as stuck, even if the whole job is allowed to take much longer.

Overlapping runs: flock

If a run hangs and cron starts the next one on schedule, you can end up with two copies fighting over the same files. flock stops that by taking a lock before the job starts:

Code
*/15 * * * * flock -n -E 75 /tmp/sync.lock /usr/local/bin/stuck-safe https://stallbell.com/p/TOKEN 12m /opt/jobs/sync.sh

With -n, flock gives up straight away if the lock is held, and -E 75 makes it exit with 75 so you can tell "skipped because the last run is still going" from a real failure. The skipped run sends no ping at all, so if the hang lasts past the next scheduled time plus grace, you also get a Missed run alert.

If a newer run does start while an older one is stuck, Stallbell closes the old run's stuck alert, since a newer run has taken over.

Test it

Point a test monitor at a job that sleeps too long:

  1. Set the monitor's Stuck after to 1 minute.
  2. Run stuck-safe https://stallbell.com/p/TOKEN 3m sleep 150.
  3. Stallbell checks every minute, so within about a minute of the 1-minute mark you get the Stuck run alert. The run then finishes after 150 seconds with exit 0, and the alert closes.

Stuck checks start once the monitor has had its first check-in, which the /start ping is.

FAQ

Does Stallbell kill the stuck job? No. It alerts. Stopping the process is timeout's job, on your server.

Why not just use timeout? It needs the server to be up and only acts after the limit. The start ping is checked from outside, so you hear about a stuck run even if the box is the problem.

Can I use this for workers and agents, not just cron? Yes. Anything that can send an HTTP request at the start and the end works the same way. See how to monitor AI agents that run on a schedule.

What if my job forgets to send the finish? Then the run looks stuck once it passes Stuck after, which is the point. Send the finish from a trap or a finally block so errors still report.

Start with the job that piles up

Pick the job that has hung before, wrap it with timeout and a start ping, and set Stuck after just above its slowest normal run.

Related: How to monitor cron jobs · How to monitor AI agents that run on a schedule · Why cron jobs fail silently · What is heartbeat monitoring?