CronNudge

Published October 21, 2026

Cron Job Works Manually but Not in Cron? Environment Variables Explained

The script runs in your terminal. The same command in crontab exits, prints nothing useful, or quietly does the wrong work. The terminal had a profile, a version manager, and the variables you exported in that session. Cron starts the job with a short list it builds itself.

This page is that list, how to set variables in a crontab, how to source an env file, the Environment= and EnvironmentFile= lines on a systemd service, and a dump you can diff. A job that never starts at all is crontab not running. The heartbeat that notices a missing finish is how to monitor cron jobs. The same gap inside a container, where Docker's environment never reaches a Debian job, is cron in Docker.

What cron puts in the environment

A user crontab does not inherit the shell where you ran crontab -e. It also does not read ~/.bashrc, ~/.bash_profile, ~/.profile, or ~/.zshrc. Anything you exported by hand, and anything a version manager added during shell startup, is absent.

Debian's crontab(5) documents SHELL, HOME, and LOGNAME. PATH is filled in by the daemon when the crontab never sets it:

Variable Default Can the crontab override it?
SHELL /bin/sh on bookworm, /usr/bin/sh on trixie Yes
HOME Home directory from the owner's /etc/passwd line Yes
LOGNAME User name from that same line No
PATH /usr/bin:/bin when the crontab never sets PATH Yes. The new value replaces the default.

On Debian, that SHELL program is dash, not bash. HOME and SHELL in the crontab replace the defaults for the jobs below that line. LOGNAME stays the crontab owner. The manual's note about USER applies to BSD, where USER is set as well. Do not assume USER on Debian. The dump at the end of this page is the list that job actually received.

The copy of /etc/crontab that ships with Debian sets a longer PATH for the lines in that file, including /usr/local/bin and the sbin directories. A user crontab does not pick that line up. On Debian, a file in /etc/cron.d does not inherit variables from /etc/crontab either (cron(8)). Set PATH in the file that holds the job.

cron(8) also reads /etc/environment when /etc/default/cron tells it to, and uses LANG, LC_ALL, and LC_CTYPE from there for the charset of cron's mail. The same paragraph says this does not affect the environment of tasks running under cron. Debian cron is built with PAM, and a PAM session can still import variables on some installs. Trust the dump, not a remembered list.

cronie, the daemon on RHEL, Fedora, Rocky, and Alma, is a different package. A PATH= line in /etc/crontab applies to that file. Dump a user job before assuming it sees the same value.

PATH

command not found is the usual symptom. node, python, php, docker, and anything under /usr/local/bin or a version manager are not on /usr/bin:/bin.

Use the absolute path the crontab's user can execute. command -v node in your interactive shell prints one path. That path has to exist for the user who owns the crontab.

0 3 * * * /usr/local/bin/node /home/deploy/app/scripts/cleanup.js

Or set PATH above the jobs. This replaces the default. It does not append to it, and Debian's crontab(5) does not expand $HOME, $PATH, or ~ in the value. Write the directories out.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 3 * * * cleanup.sh

Keep /usr/bin and /bin in that value. A version manager's shim directory moves when you upgrade. An absolute path to the binary, or a script that loads the manager on purpose, survives that better than a PATH line aimed at ~/.nvm.

SHELL and HOME

SHELL is the program that runs the crontab command line. It does not replace the shebang inside a script. #!/usr/bin/env bash still selects bash for that file. The line itself is still parsed by SHELL, so [[ ]] on the crontab line fails under dash. Set SHELL=/bin/bash when the line needs bash, or move the logic into the script.

SHELL=/bin/bash
0 * * * * [[ -f /home/deploy/flag ]] && /home/deploy/run.sh

HOME is the passwd home of the crontab owner. The job's working directory starts as that directory, so a relative path such as ./scripts/export.sh is relative to HOME, not to the folder you were in when you edited the crontab. cd in the command changes the working directory for the rest of that line. An @reboot job receives this same HOME, often before that directory is mounted: cron @reboot not working.

TZ in the crontab changes the timezone the command sees. On Debian's cron it does not move the minute the daemon fires the line. cronie's CRON_TZ does change the schedule. Both are in cron jobs and daylight saving time.

Set variables in the crontab

An assignment is its own line, NAME=value, with optional spaces around =. There is no export. Cron puts the name into the job's environment. Quotes, single or double and matching, keep leading or trailing blanks. A value with no quotes does not expand other variables.

MAILTO=ops@example.com
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
APP_ENV=production

0 3 * * * /usr/local/bin/backup.sh

Lines lower in the file see the assignments above them. A later assignment of the same name replaces the earlier one for the jobs that follow. export APP_ENV=production is not an assignment. Cron reads it as a command line, and it does not set the variable.

% in the command portion of a job line is special. It becomes a newline, and the text after the first % is stdin, unless you write \%. That rule is the command, not the assignment line. It still bites a job that builds a date or a URL inline. The full percent rules are in crontab not running.

The daemon logs the command text. A secret written into the command is copied into that log. A NAME=value line is not part of the CMD text. A file under /etc/cron.d is often mode 644, so an assignment there is readable by every local user. User crontabs live in the spool and are not world-readable. Prefer a mode 0600 file for secrets either way.

Source an env file

The crontab line runs with /bin/sh unless SHELL says otherwise. . is the POSIX source command. source is a bash builtin, and dash does not have it.

A variable assigned in that shell is not visible to the program unless it is exported. set -a exports every assignment that follows, which is what a dotenv file without export needs. set +a turns that off before the job, so only the file's names are exported.

0 3 * * * set -a; . /etc/cron-env/backup.env; set +a; /usr/local/bin/backup.sh

The file is shell syntax, because . is the shell:

# /etc/cron-env/backup.env
# mode 0600, owner is the crontab user
export DATABASE_URL='postgres://localhost/app'

export on each line also works, including under dash, and then you do not need set -a. Quote values that contain spaces. A single quote inside a single-quoted value breaks the line. Generate those files, or keep the value free of quotes.

The job user has to be able to read the file. install -m 600 -o deploy -g deploy /dev/null /etc/cron-env/backup.env creates an empty one with the right mode. Do not commit it. Do not echo it. set -x prints the expanded line.

In a container, Debian cron still will not see docker run -e. The entrypoint that writes /run/cron-env.sh is in cron in Docker.

systemd: Environment= on the service

A timer unit does not run the command and does not hold the job's environment. The service it starts does. Put the lines in the service.

# /etc/systemd/system/backup.service
[Service]
Type=oneshot
Environment=PATH=/usr/local/bin:/usr/bin:/bin
EnvironmentFile=/etc/cron-env/backup.env
ExecStart=/usr/local/bin/backup.sh

Environment= sets names for the processes the service runs. systemd does not perform shell expansion. $ is an ordinary character. Specifiers such as %h are expanded. Quote the whole assignment when the value contains spaces: Environment="APP_NAME=nightly backup".

EnvironmentFile= reads KEY=value lines from an absolute path. Empty lines, lines with no =, and lines that start with # or ; are ignored. Do not write export. The name is the text before the first =. There is no command substitution. A leading - on the path (EnvironmentFile=-/etc/cron-env/backup.env) means a missing file is not an error.

systemd.exec(5) says settings from EnvironmentFile= override Environment= for the same name. A later file overrides an earlier one. systemctl show backup.service -p Environment prints the merged list. That output includes secrets. Treat it as secret.

The file is not a shell script, so the crontab env file and the systemd file are different formats. The systemd file has no export and no set -a. The service manager reads it, so a system service can use a root-owned mode 0600 file even when User= is someone else. A cron job that sources the file needs the crontab user to be able to read it.

A system service does not inherit the manager's environment unless you pass names with PassEnvironment=. A user service manager does inherit the environment it was started with. That is still not your login shell, and it still does not read .bashrc unless something already imported those variables into the manager.

systemd.exec(5) says environment variables are a poor place for secrets. Unit files are usually mode 644, and values set with Environment= are exposed to unprivileged clients over D-Bus. LoadCredential= puts the bytes in a file under $CREDENTIALS_DIRECTORY instead. The directory is readable by the service's user and by root.

[Service]
Type=oneshot
LoadCredential=database_url:/etc/cron-env/database_url
ExecStart=/usr/local/bin/backup.sh

The script reads "$CREDENTIALS_DIRECTORY/database_url". The credential file's contents are the secret, not a KEY=value line. After any unit edit, systemctl daemon-reload. The ping for a timer is in systemd timers.

See the environment the job received

Add a one-minute line, wait for it, and read the file:

* * * * * env > /tmp/cronenv 2>&1

Diff that file against env in the terminal where the job works. The interactive list is longer. PATH, HOME, and the missing API keys are the lines that explain the failure.

Remove the crontab line when you are done, and remove the file. It contains every variable the job can see, including secrets. A new file in /tmp is created with your umask, which is often world-readable, and /tmp is sticky rather than private. A tighter line writes into the crontab user's home and keeps the file owner-only:

* * * * * umask 077; env > "$HOME/cronenv" 2>&1

$HOME in that command is cron's HOME. Quote it.

You can also reproduce the failure without waiting for the minute. This is the same idea as the check in crontab not running, with the variables limited to what a Debian job starts with:

env -i HOME="$HOME" LOGNAME="$(id -un)" SHELL=/bin/sh PATH=/usr/bin:/bin \
  /bin/sh -c '/home/deploy/backup.sh'

env -i clears the environment first. If the script fails here, it will fail under cron for the same reason. Add any NAME=value lines from the crontab to the env -i list when you want the reproduction to include them.

Where the daemon log and the job's own output go after that is where are cron logs.

A ping catches the silent failure

A missing binary is exit 127. A script with set -u exits when it touches an unset variable. Cron mails that output if a mailer exists, and discards it if one does not. The machine looks idle.

Run a wrapper by its absolute path, and always ping, including when the variable is missing. A guard that exits before curl sends nothing. The check then waits for the grace period.

#!/bin/sh
# /usr/local/bin/backup-ping
set -u
status=0
if [ -z "${DATABASE_URL:-}" ]; then
  echo "DATABASE_URL is not set" >&2
  status=1
else
  /usr/local/bin/backup.sh
  status=$?
fi
curl -fsS -m 10 --retry 3 "https://cronnudge.com/ping/<check-id>?exit=$status" >/dev/null
exit "$status"

${DATABASE_URL:-} is safe under set -u. ${DATABASE_URL:?} would end the shell before curl.

0 3 * * * /usr/local/bin/backup-ping

A non-zero ?exit= on the success URL is a failure, so the alert goes out on that ping. $URL/fail is the same signal from an error path. If cron never starts the wrapper, nothing pings, and the check goes down when the grace period ends. The default grace is 5 minutes. The form also offers none, 1 minute, 15 minutes, and 1 hour.

A job that exits 0 with the wrong database, because the variable was set and pointed at the wrong place, still looks healthy. An assertion on a count or a byte size catches an empty or tiny result. Free includes output assertions and run tracking on 3 checks. Pro removes that cap. Those signals are in assertions, runs, and agents.

Free covers 10 checks and a 5-minute minimum interval. Pro is $12 a month for 100 checks and a 1-minute minimum. Details are on pricing. Build the schedule with the cron expression tool. A message in Slack or Discord from the failure path, and what that message cannot see, is in Slack or Discord alerts when a cron job fails.

FAQ

Why does my cron job work when I run it by hand but fail under cron? Your shell loaded a profile and a PATH. Cron starts the job with its own list. On Debian, SHELL is /bin/sh (bookworm) or /usr/bin/sh (trixie), HOME comes from the crontab owner's passwd entry, and PATH is /usr/bin:/bin when the crontab does not set it. Cron does not read .bashrc or .profile. Variables you exported in the terminal are not in the job.

How do I set an environment variable in a crontab? Put NAME=value on its own line above the job. Do not write export. The assignment applies to the commands below it, and a later line replaces the same name. The value is not shell-expanded, so PATH=$HOME/bin:$PATH is stored literally. For a secret, use a mode 0600 file and source it, instead of writing the value in a world-readable /etc/cron.d file.

Why is PATH short under cron, and how do I fix command not found? Debian uses /usr/bin:/bin when the crontab never sets PATH. Commands in /usr/local/bin or a version manager are not found. Call the absolute path from command -v, or set PATH= in the crontab. That line replaces the default. It does not append, and $PATH inside the value is not expanded.

How do I load an env file from cron? Source it with the dot command and export the assignments so the script can see them: set -a; . /etc/cron-env/backup.env; set +a; /usr/local/bin/backup.sh. source is a bash builtin. Debian's /bin/sh is dash and does not have it. Keep the file mode 0600 and owned by the crontab user.

How do I see the environment a cron job actually gets? Add a one-minute line, env > /tmp/cronenv 2>&1, wait for it, and diff the file against env in your terminal. Remove the line and the file. The file contains secrets, and a file in /tmp is often readable by other users. umask 077; env > "$HOME/cronenv" keeps the dump owner-only.

Will a heartbeat catch a cron job that failed because a variable was missing? Yes, when the ping runs and the exit code is non-zero. Pass that code on the ping URL. A check that exits before curl sends nothing, and CronNudge alerts when the grace period ends. The default grace is 5 minutes. A job that exits 0 with the wrong value still looks healthy. An assertion on the result catches that. Free includes assertions and run tracking on 3 checks. Pro removes that cap.


The terminal and the crontab are different programs with different environments. Create the check, or read crontab not running for the rest of the reasons a line stays quiet. Related: where cron logs live, cron in Docker, and systemd timers.