Maaz Zindani

Loading the index…

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

A cron @reboot entry that never seems to run

Error@reboot entry in crontab -l but nothing happens after the restart

Short answer

@reboot runs the command once after the machine reboots, in cron's small environment, not your login shell. The job often runs and fails silently. Redirect its output to a file to see the real error, then use absolute paths, set variables inside the crontab, and escape any % sign.

  • Applies to cron 3.0 (Debian and Ubuntu), cronie, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the job's output to a file, so the failure is visible. Edit the entry, reboot once, and read the file.

    crontab -e
    # change the line to:
    @reboot /home/deploy/start.sh >> /home/deploy/reboot.log 2>&1
  2. 2Match what you see, then jump to its fix

    1. 1The log shows a missing program or file that exists when you run the same line in a terminalThe job runs in cron's small environmentFix
    2. 2The command is cut off after the first % and the rest arrives as inputA % sign in the command is turned into a newlineFix
    3. 3No error shown anywhere, because the output went to mail or was discardedThe job ran but its output was lostFix

What does the error look like?

  • @reboot entry in crontab -l but nothing happens after the restart
  • the script works in a terminal and fails from cron
  • no output, no mail and no error from the job

Cause 1 of 3

The job runs in cron's small environment

1Check

cron starts the command with /bin/sh, not your login shell. Only SHELL, LOGNAME and HOME are set for you, so nothing from .bashrc or .profile is loaded: no PATH additions, no nvm, no virtualenv.

@reboot env > /home/deploy/cron-env.txt 2>&1

2Fix

Use absolute paths for the program and the script, and set what the job needs at the top of the crontab. Variables are written as NAME=value on their own line.

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
@reboot /usr/bin/node /srv/myapp/server.js >> /srv/myapp/reboot.log 2>&1

3Verify

Reboot and read the log file.

sudo reboot
# after login
tail -n 20 /srv/myapp/reboot.log

Confirmed by manpages.ubuntu.com

Did this fix it?

Cause 2 of 3

A % sign in the command is turned into a newline

1Check

In a crontab, an unescaped % ends the command; everything after it is sent to the command as standard input. This hits date +%F and similar format strings.

crontab -l | grep '%'

2Fix

Escape each % with a backslash, or move the command into a script file and call the script.

@reboot /usr/bin/touch /home/deploy/started-$(/bin/date +\%F).flag

3Verify

Reboot and check that the file name contains the date.

ls /home/deploy/*.flag

Confirmed by man7.org

Did this fix it?

Cause 3 of 3

The job ran but its output was lost

1Check

cron mails a job's output to the crontab owner, to the address in MAILTO if it is set. If MAILTO is set to an empty string, no mail is sent at all, so errors disappear.

crontab -l | grep -i mailto
mail

2Fix

Redirect standard output and standard error in the entry itself, and keep the log file where the crontab's user can write it.

MAILTO=""
@reboot /home/deploy/start.sh >> /home/deploy/reboot.log 2>&1

3Verify

Reboot and read the file; it now holds the script's own messages.

cat /home/deploy/reboot.log

Confirmed by man7.org

Did this fix it?

How do I stop it coming back?

  • Redirect output to a log file in every cron entry, so a failure is never silent.
  • Use absolute paths for every program and file in the entry.
  • For a long-running service that must restart on failure, write a systemd unit instead of an @reboot line.

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.