4.4 The cron facility

1. Cron provides the ability to schedule commands or programs to run at
a future and/or regular time.  The Cron Daemon reads configuration files
that are stored in a subdirectory called /var/spool/cron/crontabs.

2. There is also the crontab command which transfers files to and
from this directory.  The crontab command lets you create, modify or
delete entries from these files. There is at most 1 file per user and
named the login userid and that userid is the owner of the file.

3. Modern cron Daemons parse all the crontab files into
a huge schedule and sleeps prior to the next scheduled command to run.
When it is time, it awakens and runs the command and goes back to sleep.

4. When the crontab command creates/updates a crontab file, the cron
Daemon automatically adds the new information to its master schedule. 

5. cron can keep a log file (e.g.: /var/cron/log or 
/var/adm/cron/log) that shows what was run and when. Logging
is turned on or off in various ways on various systems. Most 
Administrators turn logging off unless they are troubleshooting an
anomaly.

6. cron Daemons usually do not account for the time the system
is down or when the twice a year time changes occur. It is best not
to schedule commands to run between 1AM and 3AM in the U.S. because
of this.

7. Crontab file format:

Each Time-related field can contain any of:
(1) * which matches every integer.
(2) A single integer which matches itself.
(3) An (ascending) range of 2 integers separated by a dash which
	matches that range inclusively.
(4) A comma separated list of integers or ranges which matches any
	listed value.

Note that day and weekday fields are treated as an OR, not an AND
if entries are in both fields.

Comments start with a # and are not interpreted by cron. 

#minute		hour	day	month	weekday	     command
# 0-59		0-23	1-31	1-12	0-6 [0=Sun]  Any valid command
#						     run by /bin/sh

Examples:

30 18 * * 1-5	pwd	-->   Run pwd at 6:30PM Mon through Fri

0,15,30,45 * 13 * 5	banner boo --> Run banner every 15 minutes
					on Fridays and on the 13th
					of the month

0 20 1 * * mailx root << EOF %  --> At 8PM the first of each month run
	   new month: `date` % EOF	the multiline mailx command. 

20 1 * * * find /tmp -atime +3 -exec rm -f {} ';'  --> at 1:20AM daily,
							rm 3 day old or
							older tmp files

8. Editing your crontab file: 
   $ crontab katz
will install katz as your crontab file, replacing the previous version.
If katz is empty, you've just deleted your previous good crontab file.

   $ crontab -e
checks out a copy of your crontab file, invokes the editor indicated
by the EDITOR Shell variable, and then resubmits it to the crontab
directory when the editing is completed.

   $ crontab -l
lists the contents of your crontab to STDOUT

   $ crontab -r
removes the contents of your crontab file

On systems where a username and a file argument are permitted, use:
   $ crontab -u katz crontab.new

   $ crontab
will try to read a crontab from STDIN. This command is usually a mistake.
Use  *NOT*  to get a new prompt.  will erase
your existing crontab file.

9. By default, all users can submit crontab files to cron.
There are 2 configuration files: cron.allow and cron.deny
which override this policy.  If the cron.allow file exists only,
only userids within that file (one per line) can submit crontabs.
All others are not permitted. If the cron.deny file exists only,
then its list of userids are specifically denied the ability to submit
crontabs and anyone else can.  If neither file exists, only root can
submit crontabs.

10. Uses of Cron
(1) Cleaning the filesystem of garbage files
(2) distributing updated configuration files (/etc/passwd, /etc/aliases)
(3) Rotating Log files (e.g. 7 days' worth of syslog files)


11. Cron related files and directories by system:
System		Crontab Dir	Allow/Deny Dir	   Log File
Solaris	/var/spool/cron/crontabs /etc/cron.d	/var/cron/log
HP-UX   /var spool/cron/crontabs /usr/lib/cron  /var/adm/cron/log
RH Linux      /var/spool/cron    /etc		/var/log/cron
Free BSD      /var/cron/tabs	/var/cron	"Uses Syslog"


Questions? Robert Katz: rkatz@ned.highline.edu
Last Update April 22, 2003