Cron job works manually but fails in cron: the usual causes

A script that runs fine in your shell but fails from cron almost always differs in PATH, environment, working directory or shell. How to find which one and fix it.

· 6 min read · By the Spot Downtime team

You run the script by hand and it works. You add it to crontab and nothing happens, or it fails with command not found. The cause is almost always that cron does not run your job the way your login shell does. It uses a minimal environment, a different shell, and a different working directory. This post lists the differences in the order they bite most often, and how to confirm which one you are hitting.

What cron does differently

On most Linux systems, a cron job runs with:

  • A tiny PATH, typically /usr/bin:/bin. Anything installed in /usr/local/bin, ~/.local/bin, an nvm or pyenv directory or /snap/bin is not found.
  • /bin/sh as the shell, not bash. On Debian and Ubuntu that is dash, so bash-only syntax fails.
  • Your home directory as the working directory (for user crontabs), not the directory your script lives in.
  • None of the variables set by ~/.bashrc, ~/.profile or your shell session: no AWS_PROFILE, no NODE_ENV, no SSH agent, no DISPLAY.
  • No terminal. Anything that prompts for input or checks for a TTY behaves differently.

Step 1: capture the real output

Cron mails output to the local user, and on most servers nobody reads it or no mail system is installed. The first thing to do is send everything to a file:

cron
*/5 * * * * /home/deploy/bin/sync.sh >> /var/log/sync.log 2>&1

The 2>&1 matters: errors go to stderr, and without it the log shows only the normal output. Wait for the next run and read the file. Also check the system log to confirm cron actually started the job:

bash
# Debian / Ubuntu
grep CRON /var/log/syslog | tail

# RHEL / Fedora / Amazon Linux
journalctl -u crond --since "1 hour ago"

If cron never launched the job, the problem is the schedule or crontab syntax, not the environment. See cron job not running.

Step 2: reproduce cron's environment

Instead of guessing, run the script the way cron does. This starts a shell with an empty environment and cron's usual PATH:

bash
env -i HOME="$HOME" LOGNAME="$USER" SHELL=/bin/sh PATH=/usr/bin:/bin /bin/sh -c '/home/deploy/bin/sync.sh'

If it now fails the same way, you have reproduced the bug and can fix it interactively. To see exactly what variables cron provides, add a temporary job that dumps them:

cron
* * * * * env > /tmp/cron-env.txt

Compare that file with the output of env in your terminal, then remove the temporary job.

Cause 1: PATH

The most common failure is command not found for something like node, python3, aws or docker. Find where the command lives and either use the full path or set PATH in the crontab:

bash
command -v node
# /home/deploy/.nvm/versions/node/v22.11.0/bin/node
cron
PATH=/usr/local/bin:/usr/bin:/bin:/home/deploy/.nvm/versions/node/v22.11.0/bin

*/10 * * * * /home/deploy/app/run-report.sh >> /var/log/report.log 2>&1

Variable assignments at the top of a crontab apply to every job below them. They are plain assignments, so you cannot use $PATH to extend the old value: write the full list.

Pinned version paths like the nvm one above break when you upgrade. Safer is to have the script set its own PATH, or to use a wrapper that the version manager provides.

Cause 2: the shell is not bash

If the job works with bash script.sh but fails in cron with errors like [[: not found or Syntax error: "(" unexpected, cron is using /bin/sh. Either set the shell for the crontab or make the script explicit:

cron
SHELL=/bin/bash

0 3 * * * /home/deploy/bin/backup.sh

The more robust fix is a correct shebang as the first line of the script (#!/usr/bin/env bash), the executable bit set with chmod +x, and calling it by full path. A missing execute bit shows up as Permission denied in the log.

Cause 3: relative paths and the working directory

A script that reads ./config.yml or writes to logs/output.txt works when you run it from its own directory and fails from cron, where the working directory is your home. Change into the right place first, or use absolute paths everywhere:

bash
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$(readlink -f "$0")")"

./venv/bin/python export.py --config ./config.yml

set -euo pipefail makes the script stop at the first error instead of carrying on with half its work done, which also makes the log far easier to read.

Cause 4: environment variables and secrets

Credentials exported in your shell profile do not exist in cron. Typical symptoms are authentication errors from a CLI or SDK that works for you interactively. Options, from simplest to most robust:

  1. Define the variables at the top of the crontab (readable by that user, so only for low-risk values).
  2. Load them from a file the script reads, with restrictive permissions: chmod 600 /etc/myapp/env, then set -a; . /etc/myapp/env; set +a at the top of the script.
  3. Use a systemd service and timer with EnvironmentFile= instead of cron.

Don't source .bashrc in cron

Many .bashrc files return early when the shell is non-interactive, so sourcing them often does nothing, and they may print or prompt. Put what the job needs in an explicit environment file instead.

Cause 5: percent signs, quoting and permissions

  • % in the crontab. Cron treats an unescaped % as a newline. A command like date +%F breaks. Escape it as \%, or move the command into a script: 0 2 * * * tar czf /backup/site-$(date +\%F).tgz /var/www.
  • Wrong user. A job in root's crontab can see files that your user crontab cannot, and the reverse. Check that the job's user can read the files and write the output path. /etc/cron.d files need a user field after the schedule.
  • Missing newline. The last line of a crontab file must end with a newline or it may be ignored.
  • Tools that need a terminal. docker exec -it fails without a TTY; use docker exec without -t.

Overlapping runs and timezones

If the job runs but at the wrong time, or two copies run at once, the cause is different: see cron timezone and DST pitfalls and cron job overlapping runs.

Make the next failure visible

Fixing the environment solves today's problem, but a cron job that fails silently will do it again after the next PATH change, server rebuild or expired credential. Have the job tell you when it succeeds, so that silence becomes an alert. Add a ping to the end of the script, after the work that must succeed:

bash
#!/usr/bin/env bash
set -euo pipefail

/home/deploy/bin/do-the-work.sh
curl -fsS --retry 3 -m 10 https://your-heartbeat-url > /dev/null

With set -e, the ping is only reached if everything before it succeeded. A Spot Downtime heartbeat monitor waits for that request and alerts you if it doesn't arrive on schedule. For setup details, see how to set up a heartbeat monitor and why cron jobs fail silently.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading