Linux comes with two related programs for scheduling events: cron and at. Both start at boot up and run as daemons - so they never quit. cron schedules repeated events and at does one time events.
cron reads the crontab files to get its information. The system and each user have their own crontab file. The system's crontab is located at: /etc/crontab. Leave it alone. User root should create its own crontab file.
Creating root's crontab file: first, define the EDITOR variable. It is probably best to put this in /etc/profile so it is read by everyone who logs in. Open /etc/profile and add these two lines:
EDITOR=vi [Enter]If you prefer an editor other than vi, please make the appropriate change. You will have to log out and log back in to have this take effect. Then type:
export EDITOR [Enter]
crontab /etc/crontab [Enter]This will make a crontab for you that is a copy of the system's crontab. Now to edit your crontab, type:
crontab -e [Enter]Notice that crontab is both the name of the file and an executable program - similar to passwd. It will look something like this (note: this is the RedHat crontab):
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
Delete everything below the HOME=/ line as these are not the actions you want to run, and change the path to add in whatever directory you save your scripts to.
Each line in the crontab runs one program. It has a special format: five time fields followed by the program to run. Note: in the system crontab there is another field that tells cron to run the program as a specific user (e.g. root) but in a user's crontab this field is not used. The five time fields are:
minutes hours day-of-month month day-of-week
From the man page:
The time and date fields are:
field allowed values
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
A field may
be an asterisk (*), which always stands for
Ranges of numbers are allowed.
Ranges are two numbers
separated with a hyphen. The specified range is inclu-
sive. For example, 8-11 for an ``hours'' entry specifies
execution at hours 8, 9, 10 and 11.
Lists are allowed. A
list is a set of numbers (or ranges)
separated by commas. Examples: ``1,2,5,9'', ``0-4,8-12''.
Step values can be used
in conjunction with ranges. Fol-
lowing a range with ``/<number>'' specifies skips of the
number's value through the range. For example, ``0-23/2''
can be used in the hours field to specify command execu-
tion every other hour (the alternative in the V7 standard
is ``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also
permitted after an asterisk, so if you want to say ``every
two hours'', just use ``*/2''.
Names can also be used for
the ``month'' and ``day of
week'' fields. Use the first three letters of the partic-
ular day or month (case doesn't matter). Ranges or lists
of names are not allowed.
So, if we want to run our backup script at 5 minutes past 1 o'clock in the morning every day, our crontab would look like this:
5 1 * * * /usr/local/bin/run-backup
And it should email root after completion of the task telling root what happened. When you are satisfied that run-backup is running properly and you are tired of getting the emails from cron, change the MAILTO line to:
MAILTO=""For further reading, check out the man pages:
man 5 crontab