Sub topic 3
Cron's Minimal Environment and How to Work with It
This chapter assumes you can edit files, run shell commands, read exit codes, and understand basic users and permissions. You should also know that cron runs commands in the background without an interactive terminal.
Why cron jobs behave differently
Cron is a scheduler. It starts a command at a specified time, but it does not start the command inside your normal terminal session. The environment is usually much smaller.
An environment is the set of variables and settings a process receives. Your interactive shell may define PATH, HOME, language settings, cloud credentials, and application-specific variables. A cron job may receive only a few of them.
The current directory is also not guaranteed to be your project directory. A script that works because it finds ./config.yml in your terminal can fail when cron starts it elsewhere.
| Setting | Interactive shell | Cron job |
|---|---|---|
| `PATH` | Often includes user directories | Usually short and distribution-dependent |
| Current directory | Often your working directory | Not your project directory |
| Terminal | Attached to a terminal | No terminal |
| Shell startup files | Often read | Usually not read |
| User | Your login user | The crontab owner |
A useful working model is:
Set the environment explicitly
Use absolute paths for executables and files. Set PATH in the crontab when a script calls commands by name. The PATH variable is a colon-separated list of directories where the shell searches for commands.
# Edit the current user's crontab with: crontab -e
[email protected]
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
# Every 15 minutes
*/15 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1MAILTO tells cron where to send job output on systems that provide local mail delivery. Redirecting output to a log is usually easier to operate, but choose a log location that the crontab user can write.
Inside the script, use absolute paths for important files:
#!/usr/bin/env bash
set -euo pipefail
cd /srv/sync
/usr/bin/python3 /srv/sync/sync.py --config /etc/sync/config.ymlThe set -euo pipefail command makes many failures visible. -e stops after a failed command, -u detects unset variables, and pipefail makes a pipeline fail if an earlier command fails.
Check the differences directly
Create a temporary diagnostic script owned by the same user as the crontab:
#!/usr/bin/env bash
env | sort
printf 'directory=%s\n' "$PWD"
printf 'user=%s\n' "$(id -un)"Run it manually first, then schedule it for a minute or two in the future. Compare its output with env and pwd from your terminal. Remove the diagnostic entry after testing.
Common mistakes
- Calling
python,node, orawswithout confirming its cronPATH. - Relying on aliases, shell functions, or variables from
.bashrc. - Using relative paths for input, output, or lock files.
- Writing logs to a directory the job user cannot write.
- Testing as one user but installing the crontab for another user.
- Assuming a failed command will alert anyone when output is discarded.
Exercise
A script succeeds when run as /home/alex/run-report.sh in a terminal but fails from cron. Name two differences to investigate first.
Show answer
Check the command and file paths, the cron job's PATH, its current directory, and the user who owns the crontab.