Sub topic 16
Testing the job before enabling the schedule
This chapter assumes you have a wrapper script like the one in the earlier chapters (lock, timeout, log, exit status) and know how cron, systemd timers, or CI schedules start jobs. The goal is to find failures while you are watching, not at 02:00 when nobody is.
Test in layers
Each layer adds one thing that can break. Fix problems at the lowest layer first.
Layer 1: strip your shell's environment
Your interactive shell has a long PATH, exported credentials, and a working directory. The scheduler has none of these. env -i starts the script with an empty environment, so you add back only what the scheduler provides.
env -i HOME="$HOME" PATH=/usr/local/bin:/usr/bin:/bin \
AGENT_CMD=/usr/local/bin/my-agent-headless \
/home/deploy/bin/nightly-agent.sh
echo "exit status: $?"If this fails with "command not found" or an authentication error, you have found an environment problem. Fix it in the crontab, the environment file, or the CI secrets, not in your shell profile.
Layer 2: test the failure paths
A job that works once proves little. Check that the safety features fire. This script replaces the agent with harmless commands.
#!/usr/bin/env bash
# test-failures.sh
set -u
SCRIPT=/home/deploy/bin/nightly-agent.sh
run() { env -i HOME="$HOME" PATH=/usr/local/bin:/usr/bin:/bin "$@"; }
echo "Failure path:"
run AGENT_CMD=false "$SCRIPT"
echo "status: $? (expect 1)"
echo "Overlap path:"
run AGENT_CMD="sleep 20" "$SCRIPT" &
sleep 2
run AGENT_CMD=true "$SCRIPT"
echo "status: $? (expect 0, run skipped)"
tail -n 1 "$HOME/agent-logs/skipped.log"
waitTo test the timeout, temporarily change 20m in the wrapper to 5s and use AGENT_CMD="sleep 60". Expect status 124 and the timeout line in the log. Restore the real value afterwards.
Layer 3: trigger the real scheduler by hand
Each scheduler has a way to start a job now.
| Scheduler | Manual trigger |
|---|---|
| cron | Temporarily set the schedule to `* * * * *`, then restore it |
| systemd | `sudo systemctl start nightly-agent.service`, then `journalctl -u nightly-agent.service` |
| CI | Run the workflow with `workflow_dispatch` from the Actions tab |
For systemd, start the service, not the timer. This tests the unit file, the environment file, and the working directory without waiting for the clock. Also run systemctl list-timers to confirm the next trigger time matches your intent, including the time zone.
Layer 4: run with a report-only prompt
Before the agent may change anything, point the job at a copy of the prompt that says to list what it would change and to edit nothing. Combine this with read-only credentials where possible. Read the log and check that the agent found the right files, did not ask a question, and finished well inside the timeout. Then switch to the real prompt and run once on a throwaway branch.
Layer 5: watch the first scheduled run
Enable the schedule and be present for the first run. Check four things:
- The log file exists and has a timestamp.
- The exit status matches what the scheduler recorded.
- The alert path works. Deliberately break something once to confirm you are notified.
- Any branch or pull request was created once, not twice.
Exercise
Your wrapper works when you run it from your shell, but the first cron run fails with "command not found". Which test layer would have caught this, and what is the fix?
Show answer
Layer 1, running with env -i, which reproduces the scheduler's minimal environment. The fix is to set PATH (and AGENT_CMD) in the crontab, or use absolute paths in the script.