Sub topic 3
Why a job works in your shell but not under the scheduler
This chapter assumes you can write short shell scripts, know what PATH and exit codes are, and have already run a coding agent from a terminal. By the end you should be able to predict which parts of your interactive setup a scheduler will not provide, and to find the missing parts by probing instead of guessing.
The problem: your shell is doing work you don't see
A command that works at your prompt runs inside a lot of setup you never wrote into the script:
- Your login shell read
~/.profile,~/.bashrc, or~/.zshrc. These files extendPATH, load version managers such as nvm or pyenv, and export API keys. - Your terminal started in a directory you chose.
- Your session has a terminal attached, a display of environment variables, and your preferred time zone and locale.
- An SSH agent or credential helper may be running for you.
A scheduler starts the job in none of this. Cron, systemd, and CI runners all launch a non-interactive process with a small, fixed environment. The usual symptom is a job that fails at 02:00 with command not found, or one that runs but acts on the wrong directory. This hits agent jobs particularly hard. The agent's command is often installed under a user directory such as ~/.local/bin, and the agent itself spawns further tools (git, a test runner, a linter) that also have to be on PATH.
What each scheduler gives you
| Setting | cron | systemd service | GitHub Actions |
|---|---|---|---|
| `PATH` | Short, commonly `/usr/bin:/bin` | A built-in default list of system directories | The runner's `PATH` with preinstalled tools |
| Working directory | The user's home directory | `/` for system units, unless `WorkingDirectory=` is set | The checked-out workspace for `run` steps |
| Shell startup files | Not read | Not read | Not read |
| Shell | `/bin/sh` unless `SHELL=` is set in the crontab | None, since `ExecStart=` runs a program directly | Per step, `bash` by default on Linux |
| Time zone | System local time | System local time | UTC |
| Terminal attached | No | No | No |
Exact defaults vary by distribution and cron implementation. Treat the table as a list of things to check, not as guarantees. The reliable approach is to stop depending on defaults and set each value yourself.
The four things to set explicitly
1. PATH and the agent command
Use absolute paths for the agent and any tool the script calls directly. Also set PATH at the top of the crontab or script so the tools the agent launches can be found. The crontab below sets both, then runs the wrapper script:
# crontab -e
# Cron runs jobs with a minimal environment, so set PATH explicitly.
PATH=/usr/local/bin:/usr/bin:/bin
AGENT_CMD=/usr/local/bin/my-agent-headless
# Minute Hour DayOfMonth Month DayOfWeek Command
# 02:00 on weekdays, in the system's local time zone.
0 2 * * 1-5 /home/deploy/bin/nightly-agent.shIf your agent lives in a version-manager directory (for example ~/.nvm/versions/node/v22.0.0/bin), add that directory to PATH by its full path. Don't source ~/.bashrc from the script. Those files often return early when the shell is not interactive, so sourcing them silently does nothing.
2. Working directory
The wrapper script from earlier chapters does cd /srv/repos/my-project || exit 1 before running the agent. That line matters. Without it, a cron job starts in the home directory and the agent may read or edit the wrong tree, or none at all. In a systemd service you can set WorkingDirectory=/srv/repos/my-project instead. Either way, use an absolute path and make cd failure fatal.
Relative paths inside the script, such as prompts/nightly.md, only work after the cd. If the cd line moves or fails, they resolve against the wrong place.
3. Time zone
The word "02:00" means different instants under different schedulers:
- Cron and systemd timers use the system's local time zone.
- GitHub Actions schedules always use UTC.
Name the zone explicitly where the scheduler allows it. In a systemd timer you can write OnCalendar=Mon..Fri 02:00 UTC. In a script, use date -u for log file names, as the wrapper does, so logs sort correctly across zones.
Local time has a second problem. In zones with daylight saving time, 02:00 may not exist on the spring-forward night, or may occur twice on the fall-back night. Cron implementations handle this differently, so a job can be skipped or run twice. For a job that edits a repository, schedule it in UTC or pick an hour that never falls in the transition window.
Check what systemd will do before you rely on it:
systemd-analyze calendar "Mon..Fri 02:00 UTC"The output shows the normalized expression and the next time it will fire, in UTC and in local time.
4. Secrets
Interactive sessions usually have the agent's API key exported by a profile file. The scheduler does not read it. Chapter material on credentials covers safe delivery. For this chapter, the point is that a missing key looks like a failed login or an immediate non-zero exit, not like an environment problem. If the agent exits within a second, check credentials before anything else.
Find the differences instead of guessing
A probe job shows exactly what the scheduler gives you. Save this as /home/deploy/bin/env-probe.sh and make it executable:
#!/usr/bin/env bash
# env-probe.sh: record what the scheduler gives a job.
out="$HOME/agent-logs/env-probe.txt"
mkdir -p "$(dirname "$out")"
{
echo "date: $(date)"
echo "user: $(id -un)"
echo "pwd: $(pwd)"
echo "shell: ${SHELL:-unset}"
echo "path: $PATH"
echo "tz: ${TZ:-unset}"
echo "tty: $(tty 2>&1)"
command -v git jq timeout flock my-agent-headless || echo "some commands not found"
} > "$out" 2>&1Schedule it to run every minute, wait for one run, read the file, then remove the entry:
# crontab -e (temporary entry)
* * * * * /home/deploy/bin/env-probe.shCompare env-probe.txt with the same facts from your interactive shell. Any command listed as missing, or any difference in pwd or PATH, is a bug waiting to happen in the real job.
You can reproduce most of the difference without waiting for the scheduler by starting the script with an empty environment:
env -i HOME="$HOME" PATH=/usr/bin:/bin /bin/sh -c 'cd "$HOME" && ./bin/env-probe.sh'env -i discards every inherited variable, so the command sees only the ones you pass. This is faster than waiting for cron, though it is an approximation. It does not reproduce the missing terminal, and cron may set a few variables (such as LOGNAME) that you did not pass here.
For a systemd service, run it through systemd and read the journal:
sudo systemctl start nightly-agent.service
journalctl -u nightly-agent.service -n 50 --no-pagerCommon errors
- Percent signs in crontab lines. In a crontab command, an unescaped
%is treated as a newline. A line such asdate +%Ybreaks silently. Escape each one as\%, or put the command in a script, which is another reason to keep crontab lines to a single script path. - Silent failure output. Cron mails stdout and stderr to the user by default, which on a server often goes nowhere. Redirect output to a log file inside the script so failures leave a record.
- Wrong user. A system-wide crontab or systemd unit may run as root or another account whose home directory has no agent configuration. Set
User=in the unit, or install the crontab for the account that owns the credentials. - Different tool versions. Your shell may resolve
nodeorpythonthrough a version manager shim while the scheduler finds an older system version. Logcommand -vand--versionoutput at the start of each run.
Exercise
A nightly job works when you run ./nightly-agent.sh from your terminal. Under cron it fails with my-agent-headless: command not found, although the crontab line calls the script by its absolute path. Name two likely causes and one fix for each.
Show answer
First, the agent is installed in a directory that your interactive profile adds to PATH (for example ~/.local/bin), but cron's PATH is minimal. Fix: set PATH in the crontab or script to include that directory, or call the agent by its absolute path. Second, the script uses AGENT_CMD or a relative command that is defined in your shell profile, which cron never reads. Fix: export the variable in the crontab or an environment file the script loads explicitly. To confirm either cause, schedule env-probe.sh and compare its PATH with your interactive one.