crontab examples: schedule a script to run automatically
Run a script every minute, hour, day or week.
Cron runs commands on a schedule. Choose how often, enter the full path of your script, and copy the crontab line. Then add it with crontab -e.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
*/5 * * * * /home/me/backup.sh >> /home/me/cron.log 2>&1How this setup works
Step-by-step setup
- Make your script executable: chmod +x /home/me/backup.sh.
- Open the schedule: crontab -e.
- Paste the line above.
- Save and exit. Check with crontab -l.
What the important lines mean
- Your schedule */5 * * * * reads: minute, hour, day of month, month, day of week.
- */5 means every 5 units.
- >> file 2>&1 saves the output and errors to a log file.
Common errors and how to fix them
- Job does not run
- Use full paths. Cron has a small PATH, so python may not be found; write /usr/bin/python3.
- Script works by hand, not in cron
- Check the log file /home/me/cron.log and that the script is executable.
- A line with % in the command does not work
- An unescaped % in a crontab command becomes a newline, and everything after it goes to the command as input. Write \%, for example in date +\%F.
- Job runs on days I did not expect
- When both day of month and day of week are set (neither is *), cron runs the job when either matches. 30 4 1,15 * 5 runs on the 1st, the 15th and every Friday.
- */35 does not mean every 35 minutes
- A step counts inside its own field. */35 in the minute field runs at minutes 0 and 35 of every hour, so the gap is uneven.
- Last line of the crontab is ignored
- Each entry must end with a newline. If the final line has none, cron treats the file as partly broken and logs a warning to syslog.
Frequently asked questions
- How do I run a cron job every 5 minutes?
- Use */5 * * * * followed by the command. Pick "Every 5 minutes" above to get the line.
- Why is my cron job not running?
- Most often the command needs a full path, the script is not executable, or it depends on environment variables cron does not have. Send the output to a log file and read it.
- What do the five cron fields mean?
- In order: minute, hour, day of the month, month, and day of the week. A star means every value.
- How do I stop cron from sending me email?
- Set MAILTO="" at the top of the crontab. With MAILTO unset, output is mailed to the crontab owner, and with an address it goes there.
- What does @reboot do?
- It runs the command once, after the machine reboots. Other shortcuts are @hourly, @daily, @weekly, @monthly and @yearly.
- Which shell runs my command?
- /bin/sh by default. Set SHELL=/bin/bash at the top of the crontab if your command uses bash features.
Free tools that help
More server configs
- systemd service fileRun your app in the background and start it automatically at boot.
- Apache virtual hostHost a website from a folder with Apache, with or without .htaccess.
- logrotate configKeep log files from filling your disk: rotate, compress and delete old logs on a schedule.
- fail2ban jail.localBan addresses that keep failing to log in. Choose ban time, retries and which services to protect.
- PM2 ecosystem.config.jsRun a Node.js app with PM2: restarts on crash, memory limit, cluster mode and start at boot.
- WireGuard server config (wg0.conf)Set up a WireGuard VPN server on Linux with one peer, NAT and the right commands.