Every wait gets a deadline
In short: I found seven little "wait for this file" loops that had been running for 34 hours. The jobs they were waiting on had died a day and a half earlier. Nothing was broken, but it looked like runaway work and nobody could tell. The fix is boring and absolute: every wait gets a deadline, and every deadline says why it fired.

Context
A lot of what my agents do at home is long-running: a simulation for my robot side project, a CI pipeline, an audio render, a big file copy to the NAS. The agent starts the job, then waits for a result.
The natural way to write that wait, for a human or an AI, is a loop:
until [ -f results/summary.md ]; do sleep 30; done
Read it again. It has exactly one way out: the file appears. If the job crashes, is killed, writes its result somewhere else, or never starts, the loop waits forever. Run it in the background and it waits forever quietly.

That is what happened. One day I opened the task list in Claude Code and saw tasks that had been "running for 34 hours". Seven of them. Each was a background wait for a file that was never going to show up. The agents that started them had long since finished, moved on, or been closed. The loops were orphans.

They weren't using much CPU. The real cost was trust. When I glance at a task list and see 34-hour tasks, I don't know if something important is stuck, so I stop and dig. That is exactly the kind of attention I am trying not to spend.
Detail
The rule
Every background wait, whether it is an until loop, a tail -F, or a watcher tool, gets a hard limit. In shell
that is usually just timeout:
timeout 3600 bash -c 'until [ -f results/summary.md ]; do sleep 30; done' \
|| echo "gave up after 1 h: results/summary.md never appeared"
The limit is generous: longer than the job should ever take. The point is not to cut jobs short. The point is that the wait always ends, and when it ends early it says so in plain words, so the next agent or I know where to look.
The rule is in the standing instructions every agent reads, and in the briefs I give to sub-agents. It is one of the few rules I state as "always", because the failure is silent.

Photo: NASA/JPL, public domain, via Wikimedia Commons
Clean up after yourself
The second half: at the end of a job, the agent checks for leftover loops it started and stops them. That sounds simple, and it is where I hit two more surprises.
Surprise one: pkill -f can kill itself. A pattern match on the full command line also matches the shell that
is running the pkill, because that shell's command line contains the pattern. One cleanup killed its own shell
halfway through. Another wait loop never ended because its exit check matched itself.
# Risky: the calling shell's own command line contains "summary.md"
pkill -f summary.md
# Better: remember the PID when you start it, and stop exactly that
long_job & pid=$!
...
kill "$pid" 2>/dev/null

Surprise two: grep with an empty file name reads standard input. If a variable that should hold a path is
empty, grep pattern $f becomes grep pattern, which sits there waiting for input that never comes. Another quiet
hang. Quote the variable and check the file exists first:
[ -n "$f" ] && [ -f "$f" ] || { echo "no file: '$f'"; exit 1; }
grep -m1 pattern "$f"
Why this is an AI problem as much as a shell problem
None of this is new to anyone who has written shell scripts for a living. What changed is the volume. An agent writes a wait loop in a second, and it may write dozens in a day across sessions. A human who writes a sloppy loop usually notices it when they close their terminal. An agent session that ends does not look back.
So I stopped relying on the agent to remember, and wrote it down as a rule it reads every time. Since then the task list has been boring, which is what I want from it.
What I'd tell a team
- Every wait has a deadline and a message. "Gave up after 1 h, file X never appeared" saves the next person twenty minutes.
- Track what you start. Keep the PID or job ID; stop exactly that, never a pattern that might match you.
- Check inputs before you block on them. An empty variable should fail loudly, not hang politely.
- Look at your task list. If something says it has run for a day, find out why today, not next week.
- Put the rule where the agent reads it, not in your head. Agents follow written rules far better than remembered ones.
The 34-hour orphans didn't hurt anything. They just made me stop trusting what I was looking at, and that turned out to be reason enough.
Drafted with Claude.