Sub topic 7
Timeouts and exit codes: knowing when a run failed
This chapter assumes you can write short shell scripts, know that every process returns an exit status, and have run an agent in a terminal. With no one watching, a scheduled run has two ways to go wrong. It can fail, or it can hang. The scheduler can only tell the difference if your script reports it.
Exit codes are the contract
A process returns an integer when it ends. 0 means success. Any other value means failure. Cron, systemd, and CI runners all read this value. If your wrapper script always ends with exit 0, the scheduler reports success even when the agent crashed.
Two habits keep the signal honest:
- Capture the agent's status right after it runs, before any other command overwrites
$?. - End the script with
exit "$status"so the scheduler sees the same value.
A pipeline such as agent | tee log hides the agent's status behind the status of tee. Redirect output to the log file instead, as the wrapper below does.
Timeouts stop hangs
An agent can wait on a stalled network call or loop on a task it cannot finish. Without a limit, the run holds a lock and a process slot forever. The timeout command from GNU coreutils runs a command and stops it after a set time. If it stops the command, timeout exits with status 124.
#!/usr/bin/env bash
# nightly-agent.sh
# AGENT_CMD is a placeholder for your agent's non-interactive command.
# This script assumes it reads the prompt from stdin; check your agent's documentation.
set -u
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
LOG_DIR="$HOME/agent-logs"
mkdir -p "$LOG_DIR"
LOG="$LOG_DIR/nightly-$(date -u +%Y%m%dT%H%M%SZ).log"
# Skip this run if the previous one is still active.
exec 9>"$HOME/.nightly-agent.lock"
if ! flock -n 9; then
echo "previous run still active, skipping" >> "$LOG_DIR/skipped.log"
exit 0
fi
cd /srv/repos/my-project || exit 1
# AGENT_CMD is intentionally unquoted so a command with arguments splits into words.
timeout 20m $AGENT_CMD < prompts/nightly.md > "$LOG" 2>&1
status=$?
if [ "$status" -eq 124 ]; then
echo "agent timed out after 20 minutes" >> "$LOG"
fi
if [ "$status" -ne 0 ]; then
echo "nightly agent failed with status $status, log: $LOG" >&2
fi
exit "$status"The key lines are the timeout 20m call, the immediate status=$?, and the final exit "$status". A timeout is written into the log, so a reader can tell a hang from a crash.
On macOS, timeout and flock are not installed by default. GNU versions are available through Homebrew (coreutils provides gtimeout) or you can run the job on Linux.
Layer your limits
One timeout is not enough. Use several, each larger than the one inside it:
| Layer | Setting | Purpose |
|---|---|---|
| Wrapper | `timeout 20m` | Stops the agent and gives a clear status of 124 |
| systemd | `TimeoutStartSec=30min` | Stops a service whose wrapper itself hangs |
| CI | `timeout-minutes: 30` | Cancels the whole job on the runner |
Keep the inner limit shorter. The wrapper can then log the timeout before the outer layer kills everything.
Common errors
- Reading
$?too late. Anechobetween the agent andstatus=$?resets it to 0. - Success on a bad result. A zero exit only means the agent process ended normally. It does not prove the task worked. The structured output chapter covers checking results.
- Skipping with
exit 0. The lock check above exits 0 when it skips a run. That is deliberate, but it means a job that always skips looks healthy. Watch the skip log.
Exercise
Your wrapper runs timeout 20m $AGENT_CMD < prompt.md > "$LOG" 2>&1, then runs echo "done" >> "$LOG", then status=$?, then exit "$status". The agent times out. What does the scheduler see, and why?
Show answer
It sees exit status 0 (success). The echo ran between the agent and status=$?, so $? held the echo's status, not the agent's 124. Move status=$? to the line directly after the agent command.