crontab

Run a script every night at 2:00 or every 15 minutes: the crontab line explained, how to install it and check it ran.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • echo "0 2 * * * /home/deploy/backup.sh" | crontab -
  • crontab -l
  • echo "*/15 * * * * /home/deploy/backup.sh" | crontab -
  • crontab -l
  • grep CRON /var/log/syslog
  • crontab -r

How it works

cron runs commands on a schedule. Each line of your crontab is five time fields — minute, hour, day of month, month, weekday — then the command. * means "every": 0 2 * * * is 02:00 every day.

crontab -l lists your jobs. crontab FILE (or crontab - with a pipe) installs a whole list — it REPLACES the old one. Every run leaves a CRON line in /var/log/syslog.

Syntax

crontab [-u USER] { FILE | - | -l | -r | -e }

Options

OptionWhat it does
m h dom mon dowminute hour day-of-month month weekday, then the command
*/Nevery N: */15 in the minute field = every 15 minutes
FILE or -install the jobs from a file, or from a pipe
-llist your jobs
-rremove all your jobs
-eedit them in a text editor

Good to know

  • Test the script by hand first: a job that fails at 2 a.m. fails quietly.
  • Weekday 0 and 7 are both Sunday; 30 6 * * 1 is 06:30 every Monday. Server clocks often run on UTC (date).