Cron Environment Variables: Fix PATH, SHELL, and Timezones
A command that works in SSH often fails in crontab -e because cron does not start a login shell. Cron runs commands with /bin/sh, the user's HOME directory, and a short PATH (/usr/bin:/bin on Linux). Anything set in .bashrc, .profile, virtualenvs, or SSH_AUTH_SOCK is absent unless you set it yourself.
Commands below were checked on Debian 13, Ubuntu 24.04 LTS, Fedora 44 (Cronie), and FreeBSD (15.1-RELEASE manuals and 16.0-CURRENT).
What cron sets by default
Unless your crontab overrides them, cron starts each job with only these baseline variables:
Scroll horizontally to see all columns.
| Variable | Default value | Effect on your job |
|---|---|---|
HOME | Home directory from /etc/passwd | Used as the starting working directory |
LOGNAME | The job user's account name | FreeBSD also sets USER; portable Linux jobs should not assume USER is set |
SHELL | /bin/sh | Ignores the user's login shell in /etc/passwd unless overridden |
PATH | /usr/bin:/bin on Debian, Ubuntu, and Fedora; /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin via FreeBSD's default login class | Most common cause of exit 127 (command not found) |
Cron does not read ~/.bashrc, ~/.bash_profile, ~/.zshrc, or ~/.profile.
Test your job with cron's environment
You do not need to wait for the next cron minute to test a fix. Run the command as the job user under env -i so only cron's baseline variables exist:
sudo -u reports -- env -i \
HOME=/srv/reports \
LOGNAME=reports \
PATH=/usr/bin:/bin \
SHELL=/bin/sh \
/bin/sh -c 'cd /srv/reports && exec ./bin/export --dry-run'
On FreeBSD, run the same test from a root shell with FreeBSD's default PATH from the table above. Avoid * * * * * env > /tmp/cron-env.txt on shared servers when /etc/environment holds secrets. See the cron job not running guide for the rest of that workflow.
Set environment variables in a crontab
Put NAME=value lines above the schedule rows that need them:
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=ops@example.com
17 2 * * * /srv/reports/bin/export
Crontab variable lines follow strict parser rules.
Variables are not expanded
Cron does not expand variables on NAME=value lines. The daemon parses those lines itself, not a shell:
# Broken: sets PATH to the literal text "$HOME/bin:$PATH"
PATH=$HOME/bin:$PATH
Write every directory out in full, such as PATH=/srv/reports/bin:/usr/local/bin:/usr/bin:/bin.
Top-level lines apply to every job below
Each NAME=value line applies to every job below it until another line redefines that variable. To scope a variable to one job only, put it before the command, which /bin/sh parses:
17 2 * * * LC_ALL=C.UTF-8 /srv/reports/bin/export
System crontabs use the same variable format
/etc/crontab and /etc/cron.d/* use the same NAME=value lines. Only the schedule rows add the sixth username field.
Fix PATH and "command not found" (exit 127)
Exit status 127 (not found) means the binary sits outside /usr/bin:/bin, such as /usr/local/bin, /opt, ~/.local/bin, /snap/bin, or /usr/sbin.
To fix it:
- Call the binary by full path (
/usr/local/bin/restic) for a short one-command job. - Add
PATH=/usr/local/bin:/usr/bin:/binat the top of the crontab when several jobs share tools. - Put the job in a script file that sets and exports
PATHat the top.
Do not source ~/.bashrc inside a crontab line. Most .bashrc files exit early in non-interactive shells anyway, and interactive aliases do not belong in background jobs.
SHELL and HOME
Cron defaults to SHELL=/bin/sh. Bash features like set -o pipefail, [[ ... ]], and arrays fail on Debian and Ubuntu where /bin/sh is dash. Keep inline crontab commands POSIX-only, set SHELL=/bin/bash at the top of the crontab, or put the job in an executable script starting with #!/usr/bin/env bash.
HOME sets both $HOME and the starting directory. If your script reads ./config.yml, cd into the project directory first:
17 2 * * * cd /srv/reports && ./bin/export
MAILTO and job output
Cron emails stdout and stderr to MAILTO, or to the crontab owner's local Unix account when MAILTO is unset. On cloud VMs without a local mail agent, cron discards that output; see where to find and read cron logs.
MAILTO=ops@example.com
# Disable local mail attempts while keeping >> file redirects:
MAILTO=""
Cronie also supports MAILFROM= for the envelope sender. Debian, Ubuntu, and FreeBSD cron do not support MAILFROM=.
Timezones: TZ vs CRON_TZ
TZ and CRON_TZ do different things:
TZ=UTCis passed to your command. Timestamps inside your script use UTC, while Debian, Ubuntu, and FreeBSD still fire the schedule in the server's local timezone.CRON_TZ=UTCis a Cronie feature (Fedora, RHEL, Rocky, AlmaLinux) that changes when the daemon schedules entries below it. Debian, Ubuntu, and FreeBSD ignoreCRON_TZfor scheduling.
Copying a crontab with CRON_TZ=UTC from Rocky Linux to Ubuntu shifts the run time to the Ubuntu host's local clock. Check the host timezone (timedatectl on Linux, date on FreeBSD) and test schedules in the crontab syntax guide.
Host-wide files: PAM and login.conf
If the same crontab works on one host and fails on another, compare host-level environment files:
- On Debian and Ubuntu,
/etc/pam.d/cronloads/etc/environment,/etc/security/pam_env.conf, and/etc/default/localethroughpam_env. Fedora'scrondPAM stack also runspam_env(/etc/environmentand/etc/security/pam_env.conf). - On FreeBSD, cron applies the user's class from
/etc/login.conf. Personal crontabs also run through PAM, while/etc/crontaband/etc/cron.d/entries do not.
Keep secrets out of crontabs
Do not put database passwords, API keys, or private ping URLs on a crontab line. Cron logs the command string on every run. Other local users can see command arguments in ps aux while the job runs, and crontab -l output often gets pasted into tickets.
Store credentials in a chmod 600 file owned by the job user and read it inside your script:
#!/bin/sh
set -eu
umask 077
IFS= read -r API_TOKEN < /srv/reports/config/api-token
export API_TOKEN
exec /srv/reports/bin/export
Catch broken cron environments with Watchgoose
Environment bugs fail before your application starts. When a binary is missing from PATH (exit 127) or a config file is unreadable, Sentry and app loggers inside your code never load.
Adding an outbound Watchgoose ping at the end of the job catches those failures from outside the server. If a PATH or permission error stops the run, the expected ping never arrives (or the POSIX wrapper reports /127 right away; see Signaling failures). Watchgoose then alerts your notification channels.
Frequently asked questions
How do I load a .env file safely into a cron job?
Keep the file as plain KEY=value lines owned by the job user (chmod 600). Load it inside a small /bin/sh wrapper with set -a; . /srv/app/cron.env; set +a before execing your command. Avoid sourcing untrusted .env files on a crontab line.
Why do non-ASCII filenames or text tools misbehave in cron?
Minimal installs can start cron jobs in the C (ASCII) locale if /etc/default/locale (loaded by PAM on Debian and Ubuntu) or LANG is unset. Python 3.7+ switches to UTF-8 automatically under the C locale, whereas shell tools like bsdtar, sort, awk, and Ruby treat text as ASCII unless you set LC_ALL=C.UTF-8 (or an installed UTF-8 locale from locale -a) in the crontab.
Why does ssh or git fetch fail with Permission denied (publickey) in cron?
Cron does not inherit SSH_AUTH_SOCK from your interactive SSH login. Forwarded keys and user ssh-agent sessions are unavailable. Use a dedicated, passphrase-free deploy key (chmod 600) and reference it via IdentityFile in ~/.ssh/config or GIT_SSH_COMMAND.
Can spaces around = break crontab variable lines?
Write NAME=value with no spaces around = for portability. Debian cron and Cronie strip spaces around =, while strict POSIX crontab rules do not guarantee whitespace stripping around the assignment sign. Quote the value only when leading or trailing spaces belong inside it.
How do I use a Python virtualenv, nvm, or rbenv in a cron job?
Skip source .venv/bin/activate, nvm use, and rbenv init, which assume an interactive shell. Call the environment's binary directly by full path, such as /srv/app/.venv/bin/python /srv/app/manage.py clearsessions. Prepend that bin directory to PATH only if child processes invoke sibling tools.