From LQWiki
Contents |
crontab
crontab is the name of the file which is used to control the cron service, as well as a command which is used to edit this file and submit it to the crond daemon for execution.
The crontab command is most often invoked with the -e option, which launches your preferred editor as specified by the $VISUAL environment variable.
Alternatively, any text file can serve as a crontab file so long as it is properly formatted. To load that file into the computer to be executed by the crond daemon, simply execute the following command:
crontab /path/to/new/crontab/file.txt
You can list the contents of your current crontab by executing crontab -l at the command line.
Each line in the crontab file can be a comment, a variable declaration or an event line.
Comments
Comments begin with a comment mark #, and must be on a line by themselves.
Variable declarations
Variable declarations are of the form
- name=value
Unlike bash scripts, you can get away with putting spaces around the = sign. It's probably a bad habit to get into, though.
Event lines
Each event line specifies a time and a date, and a command which is to be executed them, in the format
- minute hour date month day command
The first five fields can be numbers or ranges, in the format described below. Note that you can specify either the date (i.e. within the month) or the day (of the week); the other field should be set to *.
The sixth field is a command with parameters. Percent signs -- unless escaped with a \ backslash -- will be turned into newlines, and everything after the first one of these will be fed into the command's standard input stream.
It is also possible to execute shell scripts or run various applications with cron. Let's imagine, you want to play music in the morning to awake you. If you want it to start at 6AM every weekday morning, here's the crontab line you need:
0 6 * * 1-5 /home/user/alarm.sh
Normally, the crontab file contains a MAILTO variable that directs output (stdout and stderr) to be mailed to the respective address (e.g. MAILTO=dave). If this is not working, the script may quit unexpectedly when its output has nowhere to go.
Range format
| * | Any number |
| */5 | Any number, in steps of 5 |
| 1-6 | Any number between 1 and 6 (inclusive) |
| 0-30/5 | Any number between 0 and 30, in steps of 5 |
| 1,4,9 | 1, 4 or 9 |
Months
Months can be specified in numbers or in words.
1 = jan
2 = feb
...
12 = dec
Days of the week
Days of the week also can be specified in numbers or words.
0 = Sunday
1 = Monday
2 = Tuesday
...
6 - Saturday
7 - Sunday
Examples
# fetch e-mail every ten minutes */10 * * * * fetchmail # send myself a birthday greeting 0 9 7 28 * mail -s'Happy Birthday' ajs318%Many Happy Returns - you old fart!%.%% # back up my recipe database every Monday 30 5 * * 1 mysqldump --opt recipes > /home/ajs318/backups/recipes.sql
See also
- Scheduling tasks - how to schedule tasks to run at a given time / when booting / ...

This page is available under a