Cron is the
UNIX scheduler. It's a
daemon that checks a set of files every 60 seconds, to see what's supposed to be run at that time. If it finds one, it runs it, and emails the output to you.
Cron schedules are stored in files called
crontabs. Every user on a
UNIX system can have their own, and they're pretty simple to create.
The
crontab program, when executed, will create a
temporary file, and invoke a
text editor for you to edit this file. The particular editor it uses is determined by the
VISUAL environment variable. When you've finished editing the file, crontab reads it, and applies its contents to a number of files in
/var/. It's these files that cron uses as the basis of its scheduler.
The default editor crontab uses is
vi, but to use another editor is fairly easy. Here's how you use
nano (the
GPL clone of
pico):
[jodrell@spod.uk.net jodrell]$ VISUAL="nano -bw" crontab -e
Of course you could
export the VISUAL variable in your shell's
.rc file so you can just enter
crontab -e and save some keyboard plastic.
The crontab format is pretty simple. Each scheduled task is on its own line, and there are seven columns that need to be filled in, separated by
whitespace.
MM HH DD MM DW task
MM: minute in the hour
HH: hour in the day
DD: day in the month
MM: month in the year
DM: day of the week, eg sun,mon,tue...
The minute, hour, day of the month, month and year columns are all zero-indexed, and * can be used as a wildcard. You can also use a comma-separated list, eg 0,1,2,3..., and a range eg 3-5. The days of the week can be mon,tue,wed,thu etc.
Here's an example. This entry would run
'uptime > uptime.txt' at 23:00 every day:
00 23 * * * uptime > uptime.txt
This one does a backup of /home/ every sunday:
00 00 * * sun tar zcf /home.tgz /home/
This one runs
fetchmail every hour during the working week:
00 * * * mon-fri fetchmail
By default, cron generates an e-mail report that's sent to the user everytime a scheduled task is run. You can change the destination address by using the MAILTO statement:
MAILTO="yourname@yoursite.com"
would send it to you.
MAILTO=""
would suppress the report.
This statement is applied to all the cron jobs below it in the crontab file, so you can set different recipients for different tasks.