Topic 5 of 5 · 4 min read
Watchdogs for unattended long agent jobs
Goal: Classify a long agent job as running, queued or stalled from its log and resource readings and choose the right action for each state.
A watchdog tells you which of three states a long job is in, running, queued or stalled, from its log, its CPU time and its output files, and acts on each differently. You should never find out six hours later that a job hung at minute twenty.
Check yourself · 1 of 3
Which reading shows that a long job is still running?
Why it matters
An unattended job can fail without exiting. A build waits on a hung daemon, an agent loops on the same failing command, a queue holds the job for a free slot. A completion notice never arrives, and the job holds memory and a lock the whole time.
How to do it
- Start the job with a timeout and record its PID.
timeout 2h ./run-agent.sh &thenecho $! > job.pid. A known exit code (124 means the timeout hit) is better than silence. - Start the watchdog next to it. A loop that checks every few minutes and ends when the job ends, fails, or goes quiet for longer than its normal quiet period.
- Classify with three readings.
- Running: the log grows, CPU time rises (
ps -o pid,etime,time,stat -p <pid>), or output files change. Do nothing. - Queued: the job waits for a slot, memory or a lock held by another job. Name what it waits on. Tell the owner if the wait is long. Do not restart it, you would lose the place in line.
- Stalled: no new log lines for the set time, CPU time flat, or a fatal error in the log such as an out-of-memory error. Act.
- Running: the log grows, CPU time rises (
- Act on stalled jobs safely. Check the command line first (
ps -o args= -p <pid>), confirm it is yours, thenkill <pid>. Never kill by name or port, on a shared machine that hits other people's processes. Find the cause, fix it, rerun. - Clean up at the end. A
trapthat stops daemons the job started, and a final check that none are left.
Before and after
Weak: pkill -f node when "something seems stuck". It kills every Node process of every user.
Better:
pid=$(cat job.pid)
ps -o pid,etime,time,stat -p "$pid"
ps -o args= -p "$pid" # same path, same session?
tail -n 5 job.log
kill "$pid"
What goes wrong
- Waiting for the notice. No notice comes when a process hangs.
- Confusing queued with stalled. Killing a queued job wastes its wait and may break the queue.
- Killing by pattern. The name matches other people's production processes.
- A quiet period set too short. A long test phase prints nothing for 15 minutes and the watchdog kills a healthy job.
Examples
Reading three states from one check
$ ps -o pid,etime,time,stat -p 4121
PID ELAPSED TIME STAT
4121 02:41:10 00:00:02 S
$ tail -n 2 job.log
[10:02] waiting for free memory (need 8 GB, free 3 GB)
CPU time is 2 seconds after almost 3 hours, but the log says what it waits on. This is queued, not stalled. Tell the owner about the wait and leave it alone.
A stalled job
$ ps -o pid,etime,time,stat -p 5230
PID ELAPSED TIME STAT
5230 05:58:02 00:41:19 S
$ tail -n 1 job.log
[04:12] java.lang.OutOfMemoryError: Metaspace
The last log line is six hours old and fatal. CPU time has not moved. Check ps -o args= -p 5230, confirm it is your job, run kill 5230, raise the memory setting and rerun.
Key points
- Start every long job with a timeout and a recorded PID so it cannot hang forever unnoticed.
- A watchdog is a loop that ends when the job finishes, fails or its output stops changing for a set time.
- Running means log, CPU time or output files are changing; stalled means all of them are flat or the log shows a fatal error.
- A queued job waits on something named, such as a slot or free memory, and should not be restarted.
- Stop only your own processes by PID after checking the command line, never by name or port.
Common mistakes
- Waiting for a completion notice instead of watching the job.
- Treating a queued job as stalled and restarting it.
- Using pkill or killall by name on a machine that other people use.
- Setting the quiet time shorter than the job's normal silent phases.
Check yourself · 2 of 3
A job's log ends with "waiting for a free slot (1 job ahead)". CPU time is flat. What state is it in?
Check yourself · 3 of 3
A stalled job is yours. Which stop action is safe on a shared server?
Practice items, not counted towards your mastery.
Start here
- Effective harnesses for long-running agents (Anthropic) (opens in a new tab)ArticleFree
Describes the initializer session, feature list, progress file and one-feature-per-session pattern this skill teaches.
- Effective context engineering for AI agents (Anthropic) (opens in a new tab)ArticleFree
Covers compaction, structured note-taking and sub-agent hand-offs for tasks that outgrow the context window.
- Checkpointing (Claude Code docs) (opens in a new tab)DocsFree
Explains how Claude Code snapshots edits, rewinds code and conversation, and what checkpoints do not cover.
More for this topic
- timeout(1) manual page (opens in a new tab)DocsFree
Caps a command's run time so a hung job ends by itself with a known exit code.