Run a cron job at startup with @reboot
To run a command each time the machine boots, put @reboot in place of the five time fields in your crontab: @reboot /home/alice/bin/start.sh. Cron runs it once, when the cron service starts during boot. It works in the cron that ships with Debian, Ubuntu, Fedora and RHEL, but not in hosted schedulers.
Five things to know about @reboot
Once per boot, not per cron restart
Cron leaves a marker file in/run, which is emptied at boot, and skips@rebootjobs if it's already there. Restarting the cron service doesn't run them again.- The job starts as soon as cron does, which can be before the network is up or every drive is mounted.
- No login profile is read and
PATHis short, so use full paths to commands and scripts. Output goes nowhere you'll see
There's no terminal at boot. Redirect output to a log file, or cron tries to mail it to you.- GitHub Actions, Kubernetes, AWS EventBridge, Vercel and Jenkins don't accept @reboot.
Add a job that runs at boot
Open your crontab with crontab -e, add a line that starts with @reboot followed by the command, as in the example crontab, and save. The job runs as you. To run it as root, edit root's crontab with sudo crontab -e.
In the system crontab, /etc/crontab, and in files under /etc/cron.d, every line names the user to run as after the schedule:
# /etc/cron.d/startup: system crontabs name the user
@reboot root /usr/local/bin/mount-backups.shWhen @reboot runs, and when it doesn't
@reboot runs when the cron daemon starts, but only the first time after a boot. Debian and Ubuntu's cron creates /run/crond.reboot as it runs the jobs, and cronie (Fedora, RHEL, Arch) creates cron.reboot in its run directory. If the file already exists, cron skips the jobs. /run is emptied at every boot, so the jobs run once per boot, and systemctl restart cron doesn't run them again.
To test a job without rebooting, run its command by hand in a minimal shell, such as env -i /bin/sh -c 'your command', which is closer to what cron gives it than your terminal is.
Wait for the network
Cron starts early, and nothing makes it wait for the network, a mounted drive or a database. A script that downloads something, connects to a server or reads from /mnt can fail on boot and then work when you run it by hand. The simple fix is a delay in front of the command: @reboot sleep 60 && /path/to/script. A script can also wait in a loop until what it needs answers.
If the job has to start after something specific, or should restart when it crashes, a systemd unit is the better tool. It can wait for network-online.target, and systemctl enable sync.service runs it at every boot:
# /etc/systemd/system/sync.service
[Unit]
Description=Sync on boot
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
User=alice
ExecStart=/usr/bin/python3 /home/alice/sync.py
[Install]
WantedBy=multi-user.targetUse full paths
Cron doesn't read your .bashrc or .profile. It runs commands with /bin/sh and a short PATH (Debian's cron uses /usr/bin:/bin), so node or docker in /usr/local/bin may not be found. Write full paths, or set PATH= on a line of its own at the top of the crontab. A % in the command is turned into a newline unless you escape it as \%.
Check that it ran
Cron logs each job it starts. On Debian and Ubuntu, look with journalctl -b -u cron (the -b limits it to this boot); on Fedora and RHEL the service is crond. The log shows that the command started, not whether it worked.
For that, redirect the job's output to a file with >> /path/to/log 2>&1. Otherwise cron mails the output to the crontab's owner, and on most machines that mail goes nowhere.
@reboot not running? Check these
Start with what cron logged during this boot. Debian and Ubuntu's cron writes Running @reboot jobs when it starts them, and every cron writes a CMD line for each job it starts. No CMD line means cron never started the job; a CMD line means it started and the command failed.
# Is cron started at boot? (crond on Fedora and RHEL, cronie on Arch)
systemctl is-enabled cron
# What cron logged about @reboot since this boot
journalctl -b -u cron | grep -i reboot
# Which jobs it started
journalctl -b -u cron | grep CMDThen match what you see:
| What you see | Cause | Fix |
|---|---|---|
| Nothing in the log since the boot | The cron service isn't enabled, so it didn't start at boot. | sudo systemctl enable --now cron (crond on Fedora and RHEL), then reboot. |
| Nothing ran after you saved the crontab | @reboot jobs don't run when the crontab changes, only the next time cron starts after a boot. | Reboot, or run the command by hand to test it. |
Skipping @reboot jobs -- not system startup | Debian and Ubuntu: cron was restarted, not the machine, and its marker file says the jobs already ran this boot. cronie logs @reboot jobs will be run at computer's startup. | Reboot the machine. Restarting cron never runs them. |
| A CMD line, but the job failed; it works by hand | It ran before the network, a mounted drive or a database was ready. | Add sleep 60 && or use a systemd unit. |
command not found in your log file | Cron's PATH is short and your .bashrc isn't read. | Write full paths, such as /usr/local/bin/node. |
Permission denied | The script isn't executable. | chmod +x the script, or call it through its interpreter: /bin/bash /path/to/script.sh. |
| The command is cut short or fails with a syntax error | A % in the line, as in date +%F, ends the command there. | Escape it as \%, or move the command into a script. |
| Scripts in your home directory fail until you log in | An encrypted home directory, like Ubuntu's ecryptfs option, is only mounted when you log in. | Keep the script and its files outside it, in /usr/local/bin or /opt. |
| A desktop app or window never appears | At boot there's no desktop session to show it in, so a GUI program has no display to open. | Use the desktop's autostart instead: a .desktop file in ~/.config/autostart, or its Startup Applications setting. |
Start, stop or restart the cron service
Cron runs as a system service, and its name depends on the distribution: cron on Debian and Ubuntu, crond on Fedora and RHEL, and cronie on Arch. To restart it:
# Debian and Ubuntu
sudo systemctl restart cron
# Fedora, RHEL, Rocky and Alma (cronie)
sudo systemctl restart crond
# Arch (cronie)
sudo systemctl restart cronie
# No systemd, as in WSL without systemd enabled
sudo service cron restart| To | Debian and Ubuntu | Fedora, RHEL, Rocky, Alma |
|---|---|---|
| Start | sudo systemctl start cron | sudo systemctl start crond |
| Stop | sudo systemctl stop cron | sudo systemctl stop crond |
| Restart | sudo systemctl restart cron | sudo systemctl restart crond |
| Is it running? | systemctl status cron | systemctl status crond |
| Start now and at every boot | sudo systemctl enable --now cron | sudo systemctl enable --now crond |
You rarely need to. Cron notices a crontab saved with crontab -e within a minute, and changes to /etc/crontab and /etc/cron.d too. Restart it after changing the server's time zone, so schedules follow the new zone. A restart doesn't run @reboot jobs: they run once per boot.
A Docker container has no service manager, so nothing starts cron for you. Run it in the foreground as the container's command. On macOS, launchd starts cron by itself when a crontab exists, so there's no service to start.
# Docker: run cron in the foreground as the container's main process
cron -f # Debian and Ubuntu images
crond -f # Alpine (BusyBox crond)Reboot the machine on a schedule
@reboot runs a job after a reboot. To make cron reboot the machine, use an ordinary schedule and the shutdown command, in root's crontab (open it with sudo crontab -e), since only root may reboot:
# sudo crontab -e (root's crontab)
# Reboot every day at 04:00
0 4 * * * /usr/sbin/shutdown -r now
# Or every Sunday at 04:00, warning logged-in users 5 minutes ahead
55 3 * * 0 /usr/sbin/shutdown -r +5 "Weekly reboot"Write the full path, /usr/sbin/shutdown. A root crontab gets the same short PATH as any other, /usr/bin:/bin on Debian and Ubuntu, so a bare shutdown or reboot isn't found and the job fails without a word. /usr/bin/systemctl reboot works too.
shutdown -r +5 waits five minutes and warns logged-in users first, so the second line starts at 03:55 to reboot at 04:00. The time is in the server's time zone, which on cloud machines is often UTC. Don't put the reboot on an @reboot line: the machine would restart in a loop.
@daily, @hourly and the other shortcuts
Cron accepts a few more names in place of the five fields. Unlike @reboot, each is a plain schedule, in the server's time zone:
| Shortcut | Same as | Runs |
|---|---|---|
@yearly or @annually | 0 0 1 1 * | At 00:00 on the 1st of January. |
@monthly | 0 0 1 * * | At 00:00 on the 1st of every month. |
@weekly | 0 0 * * 0 | At 00:00 on Sundays. |
@daily or @midnight | 0 0 * * * | At 00:00 every day. |
@hourly | 0 * * * * | Every hour, on the hour. |
Hosted schedulers don't run @reboot
@reboot. GitHub Actions, AWS EventBridge and Vercel take only the five-field form, so write 0 0 * * * rather than @daily. Jenkins has the shortcuts but hashes their times, so its @daily isn't midnight.