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 environment variables and exit codes are, and have run an agent interactively. It covers the three differences that break most first scheduled runs: the environment, the working directory, and the time zone.
What your shell gives you for free
When you type a command, your shell has already read your profile files (~/.bashrc, ~/.zshrc, ~/.profile). Those files add directories to PATH, load version managers, and export API keys. You are also standing in a project directory, and your time zone is set.
A scheduler starts none of that. Cron and systemd launch a plain, non-interactive process with a minimal environment. A command that works when you type it can fail with command not found, or it can run in the wrong place.
The three differences
| Difference | In your shell | Under the scheduler |
|---|---|---|
| `PATH` | Includes tools from your profile | Often only `/usr/bin:/bin` |
| Working directory | Where you typed the command | Usually your home directory, or `/` for services |
| Time zone | Your local zone | Cron uses the system zone, GitHub Actions uses UTC |
Environment variables you exported in a profile are also missing. Chapter material on credentials covers how to supply secrets. Here, remember only that nothing is inherited.
Setting the environment explicitly
In a crontab, define PATH and any settings at the top, and use absolute paths for the 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.shIn systemd, set the directory and environment in the service unit.
# /etc/systemd/system/nightly-agent.service
[Unit]
Description=Nightly coding agent run
[Service]
Type=oneshot
WorkingDirectory=/srv/repos/my-project
EnvironmentFile=/etc/nightly-agent.env
ExecStart=/home/deploy/bin/nightly-agent.sh
TimeoutStartSec=30minThe wrapper script from the earlier example also calls cd /srv/repos/my-project || exit 1. Doing this inside the script means the job works the same under cron, systemd, and CI.
Reproducing the scheduler's environment
Before you schedule anything, run the script with a cleared environment. env -i starts a process with no inherited variables.
env -i HOME="$HOME" PATH=/usr/bin:/bin /bin/sh -c '/home/deploy/bin/nightly-agent.sh'
echo "exit status: $?"If this fails with command not found or AGENT_CMD unset, the scheduler will fail the same way. Fix it here, where you can see the error.
Time zones
Cron evaluates 0 2 * * 1-5 in the system's local time zone. GitHub Actions evaluates schedules in UTC. A job at "02:00" can therefore run at different wall-clock times on different systems. In scripts, use date -u for log names, as the wrapper does, so logs sort the same way everywhere.
Daylight saving time can make a local-time job run twice or not at all on the changeover night. If that matters, schedule in UTC.
Exercise
A script runs correctly from your terminal, but under cron the log says "my-agent-headless: command not found". Name two fixes.
Show answer
Set PATH at the top of the crontab to include the directory that holds the agent (for example /usr/local/bin), or call the agent by its absolute path, for example through AGENT_CMD=/usr/local/bin/my-agent-headless. Confirm the fix by running the script under env -i.