Find Out Why a cron Job Is Not Running — Logs, PATH and %
A job works in the terminal but not from cron. Read the log to see if cron started it, then check PATH, %, permissions, a missing newline and the user field.
The crontab line is there but nothing happens. First decide whether cron ran the line at all, or ran it and the command failed. The steps below follow the man pages of cron 3.0pl1, the cron package in Ubuntu 24.04.
[1] Look for the job start in the log. cron logs to the syslog facility cron, and by default it records only the start of a job (-L in cron(8), default 1).
$ journalctl -u cron --since "1 hour ago"$ grep CRON /var/log/syslog[2] Know the line shape. A started job appears as CRON[PID]: (user) CMD (command). The man page says cron's child processes are renamed in uppercase, as seen in syslog and ps, and the log format string inside the cron binary is (%s) %s (%s). The line below shows that shape; it is not copied from a real log.
… server1 CRON[PID]: (deploy) CMD (/home/deploy/bin/mybackup)- A
CMDline exists: cron ran it. Go to step 3 and check the environment and the command. - No
CMDline: cron did not read the line, or the time did not match. Go to step 7.
[3] Run the command in the environment cron gives it. That environment is nearly empty, so anything that only works in a login shell fails. env -i with cron's default PATH reproduces it; the cron binary contains /usr/bin:/bin as the default.
$ PATH=/home/deploy/bin:$PATH sh -c 'mybackup'backup ok$ env -i SHELL=/bin/sh PATH=/usr/bin:/bin HOME=/home/deploy LOGNAME=deploy /bin/sh -c 'mybackup'/bin/sh: 1: mybackup: not foundThat pair is the "works in the terminal, fails in cron" case.
[4] Use an absolute path, or set PATH at the top of the crontab. The absolute path passes in the same environment.
$ env -i SHELL=/bin/sh PATH=/usr/bin:/bin HOME=/home/deploy /bin/sh -c '/home/deploy/bin/mybackup'backup okcrontab(5) states that variable values are not expanded: PATH=$HOME/bin:$PATH and PATH=~/bin:/usr/bin will not work as you might expect. Write the full value.
PATH=/home/deploy/bin:/usr/local/bin:/usr/bin:/bin5 0 * * * mybackup >> /home/deploy/backup.log 2>&1[5] To see the real environment, put env > /tmp/cron-env.txt on the first line of the script and open the file after the next minute. Below is the same line run under env -i.
$ cat /tmp/cron-env.txtHOME=/home/deployLOGNAME=deployPATH=/usr/bin:/binSHELL=/bin/shPWD=/home/deploy[6] Check the script itself. Two cases, reproduced in the same environment.
$ env -i PATH=/usr/bin:/bin HOME=/home/deploy /bin/sh -c '/home/deploy/scripts/report.sh'/bin/sh: 1: /home/deploy/scripts/report.sh: Permission denied$ env -i PATH=/usr/bin:/bin HOME=/home/deploy /bin/sh -c '/home/deploy/scripts/crlf.sh'/bin/sh: 1: /home/deploy/scripts/crlf.sh: not foundThe first is a missing execute bit: run chmod +x, or put the interpreter in front (/bin/sh /home/deploy/scripts/report.sh). The second says not found for a file that exists, because a Windows line ending (\r) after the shebang makes the kernel look for /bin/sh\r. Confirm with file script (it reports CRLF) and fix with sed -i 's/\r$//' script.
[7] Suspect the crontab line. By symptom:
| Symptom | Cause | Source |
|---|---|---|
| No newline at the end | A last entry ending at EOF makes the crontab "(at least partially) broken"; a warning goes to syslog | crontab(5) DIAGNOSTICS |
% in the command | An unescaped % becomes a newline, and everything after the first % is sent as standard input | crontab(5) |
| Both day fields set | If neither is *, the job runs when either matches | crontab(5) Note |
| User field in a user crontab | The user field exists only in /etc/crontab and /etc/cron.d | crontab(5) |
Dot in an /etc/cron.d file name | Only letters, digits, _ and - are allowed; files with dots are ignored | cron(8) DEBIAN SPECIFIC |
/etc/cron.d owner or mode | Must be owned by root and not group- or other-writable | cron(8) NOTES |
% bites when a file name contains a date. Write date +\%F instead of date +%F; the man page example escapes it the same way (d=$(date +\%e)).
A user-field mistake turns the user name into the command. With 5 0 * * * root /home/deploy/bin/mybackup in a user crontab, root is run as the program:
$ env -i PATH=/usr/bin:/bin /bin/sh -c 'root /usr/bin/true'/bin/sh: 1: root: not found[8] Find out where the output goes. cron(8) says command output is mailed to the crontab owner, or to the user in MAILTO. With no mail set up, the error text ends up nowhere visible. Send it to a file.
5 0 * * * /home/deploy/bin/mybackup >> /home/deploy/backup.log 2>&1MAILTO="" sends no mail at all (crontab(5)).
[9] You do not need to restart cron after a fix. Per cron(8), it checks the modification time of the spool directory and /etc/crontab every minute and reloads only what changed. Look at the log from step 1 again after a minute or two.
Check it with the cron · systemd time expressions tool ↗ — calculated in your browser.