Running programs periodically
There is also a facility to specify, for instance, 'run program
X every morning at 2 am'. This is enabled by the command
crontab . The mechanism used by crontab is
different to that for at or batch .
For each user, a file is kept on the system to specify which
commands are to be run periodically, and when. You can edit this
file using vi by invoking crontab with
option -e ('edit'). Each line of this file commences
with five columns, representing respectively minutes (0--59), hours
(0--23) day of month (1--31), month (1--12), and day of week
(1--7); each of these columns contains either a number, a
comma-separated list of numbers, or an asterisk. The rest of each
line is a command to be executed repeatedly whenever the five
columns match the current time and date. An asterisk means 'every'.
For instance, if the crontab file contains
30 15 * * * ls -R
0 0 * * 1 X
0 0 * 6 * Y
0 0 1,8,15,22 * * Z
then the command ls -R will be executed every day
at 3:30 pm, command X will be run first thing every
Monday (which is day 1 in the week), and command Y
first thing every day in June. Command Z is run on
days 1, 8, 15 and 22 of each month. If you try to create an entry
in the crontab file that is inconsistent, such as
specifying a non-existent date, you will be warned and the
crontab file will not be changed.
To list the entries in your crontab file without
using the editor type crontab -l .
Worked example
Create an entry in your crontab file to send user
jo a friendly message every Christmas morning.
Solution: Using crontab -e create a
line in the file that is
00 09 25 12 * echo Happy Xmas | mailx jo
indicating that the message Happy Xmas will be
piped to the mail program mailx at 0900 hours on the
25th of the 12th month each year. The day field is left as a
* since having specified the date we do not need to
worry about the day of the week as well.
Some sites will restrict the use of this command - if you find
difficulties, check with your system administrator first.
|