Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A cron job on EC2 never runs, or runs and does nothing

ErrorCannot get cron to work on Amazon EC2

Short 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.

  • Applies to Ubuntu 24.04, Amazon Linux 2023, cronie and vixie-style cron
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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>&1
  2. 2Match what you see, then jump to its fix

    1. 1The job runs but the command is cut short, typically in a date format such as date +%FAn unescaped % character in the commandFix
    2. 2Nothing runs after you edited the file with a script or echo, and a warning appears in syslogThe last crontab line has no newlineFix
    3. 3The 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. 4The job is in /etc/crontab or /etc/cron.d and never runsA system crontab entry without the user fieldFix

What does the error look like?

  • Cannot get cron to work on Amazon EC2
  • A 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/www

3Verify

Run it every minute for a moment and look at the log file.

ls -l /backup/

Confirmed by man7.org

Did this fix it?

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 -c

2Fix

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.log

Confirmed by man7.org

Did this fix it?

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.txt

2Fix

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>&1

3Verify

Wait for the next run and check the log for the real output.

tail -n 20 /var/log/app-job.log

Confirmed by man7.org

Did this fix it?

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>&1

3Verify

Wait for the next run and read the log.

tail -n 20 /var/log/app-job.log

Confirmed by man7.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.