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 herenothing runs on your computer
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
| Option | What it does |
|---|---|
m h dom mon dow | minute hour day-of-month month weekday, then the command |
*/N | every N: */15 in the minute field = every 15 minutes |
FILE or - | install the jobs from a file, or from a pipe |
-l | list your jobs |
-r | remove all your jobs |
-e | edit 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 * * 1is 06:30 every Monday. Server clocks often run on UTC (date).