Sub topic 10
Keeping logs and run records
This chapter assumes you can write a wrapper script that applies a timeout and returns the agent's exit status. It also assumes you know basic jq usage. Nobody watches a scheduled run, so the only evidence of what happened is what you saved.
Two kinds of records
A scheduled job needs two records, and they do different work.
| Record | Contents | Used for |
|---|---|---|
| Run log | Full agent output (stdout and stderr) for one run | Finding out why a run failed or did something odd |
| Run record | One short line per run: ID, times, exit status, log path | Seeing at a glance whether runs happen and succeed |
The log is large and read rarely. The run record is small and can be scanned or queried by other tools, such as the alerting you add later.
What to capture
Give each run a unique ID. A UTC timestamp works well because it sorts correctly and avoids daylight-saving ambiguity. Use it in the log filename so a record can always point to its log.
Capture both output streams, because many agents write progress and errors to stderr. Redirect with > "$LOG" 2>&1.
For each run, record at least:
- the run ID
- the start and end times
- the exit status
- the path to the log
- the prompt file used, so you can tell which version of the instructions ran
A wrapper with a run record
This script extends the nightly wrapper. It appends one JSON object per run to a file in JSON Lines format, then removes old logs.
#!/usr/bin/env bash
# nightly-agent-logged.sh
set -u
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
LOG_DIR="$HOME/agent-logs"
mkdir -p "$LOG_DIR"
RUN_ID="$(date -u +%Y%m%dT%H%M%SZ)"
LOG="$LOG_DIR/nightly-$RUN_ID.log"
PROMPT="prompts/nightly.md"
cd /srv/repos/my-project || exit 1
started="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
# AGENT_CMD is intentionally unquoted so a command with arguments splits into words.
timeout 20m $AGENT_CMD < "$PROMPT" > "$LOG" 2>&1
status=$?
ended="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
jq -nc \
--arg run_id "$RUN_ID" \
--arg started "$started" \
--arg ended "$ended" \
--arg log "$LOG" \
--arg prompt "$PROMPT" \
--argjson status "$status" \
'{run_id: $run_id, started: $started, ended: $ended,
status: $status, log: $log, prompt: $prompt}' >> "$LOG_DIR/runs.jsonl"
# Keep 30 days of logs. The run records stay.
find "$LOG_DIR" -name 'nightly-*.log' -mtime +30 -delete
exit "$status"To see recent failures, run jq -c 'select(.status != 0)' ~/agent-logs/runs.jsonl.
Where the scheduler's own logs fit
The scheduler keeps its own records, and they answer a different question: did the job start at all? With systemd, journalctl -u nightly-agent.service shows start and stop events and anything the script printed. Cron mails output or discards it, depending on configuration, so do not rely on it. A CI system keeps a log for each run, but only for a limited time. Your own files are the record you control.
Common mistakes
- Writing the log to a path that changes with the working directory. Use absolute paths.
- Overwriting one log file each run, which erases the evidence of the last failure.
- Never deleting logs, which eventually fills the disk and makes later runs fail.
- Recording only success. A run that crashes before the record is written leaves no trace, so a missing line is itself a signal to alert on.
Exercise
A job's run record shows status 124, and the log ends mid-sentence. What most likely happened, and which fields in the record help you confirm it?
Show answer
The timeout command killed the agent because the run exceeded its limit (124 is timeout's exit status on expiry). Compare the started and ended fields: their difference should be close to the timeout, here 20 minutes. Then open the file named in the log field to see where the output stopped.