Crontab

From LQWiki
Jump to: navigation, search

Overview

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" (without the quotes) 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> <day of month> <month> <day of week> [user] <command>

The first five fields can be numbers or ranges, in the format described below. Note that you can specify either the day of month or the day of week; the other field should be set to *. The [user] field is optional; the command will then run as if user had called it.

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 possile to execute shell scripts or run various applications with cron. One might, for instance, write a simple shell script to play a certain music file as, say, a morning alarm. If we wanted this alarm to go off at 6AM every weekday morning, the line in the crontab file to do this might look like this:

0 6 * * 1-5 /home/user/alarm.sh

However, this can sometimes be problematic when there is no specification (or poor specification) as to where the script's output (Stdout and Stderr) should go. Normally, the crontab file contains a MAILTO variable that directs output to be mailed to the username to which that varibale is set (e.g. MAILTO=dave), but if this is not working, then there can be problems. The script may quit unexpectedly when its output has nowhere to go.

It is wise, therefore, to direct output to some file, usually the null device. In this case, the above crontab event line might look like this:

0 6 * * 1-5 /home/user/alarm.sh >> /dev/null 2>&1

This directs both standard output and standard error output to go to the null device (i.e. the trash).

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
3 = Wednesday
4 = Thursday
5 = Friday
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