A cron job on EC2 never runs, or runs and does nothing
Cannot get cron to work on Amazon EC2Short answer
Cron runs commands with a minimal environment through /bin/sh. The documented traps are an unescaped % character, which cuts the command and turns the rest into input, a crontab whose last line has no newline, a missing user field in system crontabs, and scripts that rely on a PATH or HOME that cron does not set up.
Fix it fast
1Find out which cause you have
Make the job log its own output, so every later cause becomes visible. Run it every minute while you debug.
crontab -l # temporary test entry: # * * * * * /home/ubuntu/job.sh >> /home/ubuntu/cron-test.log 2>&12Match what you see, then jump to its fix
- 1
The job runs but the command is cut short, typically in a date format such as date +%FAn unescaped % character in the commandFix - 2
Nothing runs after you edited the file with a script or echo, and a warning appears in syslogThe last crontab line has no newlineFix - 3
The script works in your shell, but under cron it says command not found or cannot find its filesThe script depends on an environment that cron does not give itFix - 4
The job is in /etc/crontab or /etc/cron.d and never runsA system crontab entry without the user fieldFix
- 1
What does the error look like?
Cannot get cron to work on Amazon EC2A warning will be written to syslog.
Cause 1 of 4
An unescaped % character in the command
1Check
In a crontab the command ends at the first unescaped %. Everything after it is sent to the command as standard input, and further % characters become newlines.
crontab -l | grep '%'2Fix
Escape each % with a backslash, or move the command into a script file and call the script.
# inside the crontab
0 2 * * * /usr/bin/tar -czf /backup/site-$(date +\%F).tgz /var/www3Verify
Run it every minute for a moment and look at the log file.
ls -l /backup/Confirmed by man7.org
Cause 2 of 4
The last crontab line has no newline
1Check
cron requires each entry to end in a newline. If the last entry stops at end of file, cron treats the crontab as at least partly broken and writes a warning to syslog.
crontab -l | tail -c 1 | od -c2Fix
Add a newline after the last entry, and edit with crontab -e rather than writing the file by hand.
(crontab -l; echo '') | crontab -3Verify
Wait for the next minute mark and read the log.
tail -n 5 /home/ubuntu/cron-test.logConfirmed by man7.org
Cause 3 of 4
The script depends on an environment that cron does not give it
1Check
Cron sets only a few variables itself: SHELL is /bin/sh, and LOGNAME and HOME come from the owner's passwd entry. Your login shell's own settings are not loaded.
# temporary entry to see what cron provides:
# * * * * * env > /home/ubuntu/cron-env.txt 2>&1
cat /home/ubuntu/cron-env.txt2Fix
Use absolute paths for every command and file, or set variables at the top of the crontab.
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
*/5 * * * * /usr/bin/node /srv/app/job.js >> /var/log/app-job.log 2>&13Verify
Wait for the next run and check the log for the real output.
tail -n 20 /var/log/app-job.logConfirmed by man7.org
Cause 4 of 4
A system crontab entry without the user field
1Check
Jobs in /etc/crontab and /etc/cron.d are system jobs, so each line needs a user name after the five time fields. User crontabs made with crontab -e do not have that field.
cat /etc/cron.d/*2Fix
Add the user name, or move the line into the user's own crontab.
# /etc/cron.d/job
*/5 * * * * ubuntu /usr/bin/node /srv/app/job.js >> /var/log/app-job.log 2>&13Verify
Wait for the next run and read the log.
tail -n 20 /var/log/app-job.logConfirmed by man7.org
How do I stop it coming back?
- Keep job logic in a script with absolute paths, and keep the crontab line to one short call.
- Always redirect output to a log file, or set MAILTO deliberately, so failures leave a trace.
- Edit with crontab -e, and test a new job with an every-minute schedule before changing it to the real one.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix