User:Spoonbender/Temp/Cron.doc

The cron Service

 * UNIX provides the cron daemon for running processes automatically.
 * The cron daemon is started during the system boot process and executes time scheduled jobs submitted by the system administrator and other users.
 * Regular users, as well as root, can utilize cron for repetitive execution of programs
 * Jobs are submitted to cron with the crontab command
 * Root controls who can use crontab through the /var/adm/cron/cron.allow file
 * Users are only permitted to use the crontab if they are listed in the cron.allow file

If cron.allow does not exist, then /var/adm/cron/cron.deny is checked to determine if the user should be denied access. Considering the cron.allow and cron.deny files:

The cron File
Every user that is granted permission to use the cron service is given a configuration file in the /var/spool/cron/crontabs directory. The cron daemon consults these files to determine what scheduled jobs should be run and when it should be run. The cron file contains one job schedule entry per line in the file, each line containing several fields that must conform with the structure of the cron file.

Each line in the cron file consists of six fields, each separated by white spaces or tabs. The first five fields are digits representing the time when the command on the sixth field would be executed. The values that are accepted in the first five fields are integer values of the minute, the hour, the date, the month, and the day of the week. These could be a single value integer, a range of integers defined by a dash, a list of integers, each separated by commas, or an asterisk, which represents all possible values for a field.

The sixth field specifies the command that should be executed at the specified times. Remember that you must redirect the output of the command into standard output or standard error, or else, the result of the command would be mailed to you. Also, remember that when placing a command in the field, always use a full path name for the command and the output file. cron only uses that standard environment of the /usr/bin/sh and does not know the currently loaded environment.

An example of the cron file is given below.

 # min hour date month day command 

 # 0-59 0-23 1-31 1-12 0-6 be sure to redirect output 

0 * * * * /usr/bin/date &gt; /home/user/date.now

0 6 1,15 * * &gt; /var/adm/wtmp

30 8 * * 1-6 /usr/bin/who | /dev/console

The examples above show the common usage of the cron service. The first example (third line, lines with # are omitted from execution), executes the /usr/bin/date command and redirects its output to the /home/user/date.now file every 0th minute of every hour, every date of every month, on an everyday basis.

The second example trims down the contents of the /var/adm/wtmp file every 6:00 am on the first and fifteenth day of the month. The third example lists all the users logged onto the system every 8:30 am on Mondays thru Saturdays.

Remember that minute 0 means x:00, hour 0 means 12:xx am, hour 13 means 1:00 pm, months 1 – 12 represents January – December, days 0 – 6 represents Sunday – Saturday. An asterisk (*) represents all values that would be valid for that field.

Managing cron Schedules
To create, edit, remove and list jobs scheduled for execution using the cron service, we need to use the crontab command. The following are the only options recognized by crontab:

 # crontab –e 

Used to create and edit your cron file. After executing the command, your cron file is temporarily placed in a vi session. Using the vi editor, you could add, edit and delete entries in the file. After saving the changes in the vi editor, your cron file is automatically updated. Remember that while the vi session is not saved, the old cron file would still be consulted by the cron daemon.

 # crontab –l 

This command would list all the valid lines in your cron file.

 # crontab –r 

This command would remove all the entries in your cron file. Remember that this purges all of your scheduled jobs. If you want to delete only a single or a couple of lines in your cron file, only use the crontab –e command.

If your are logged in as root, you could modify any of the users’ cron file by using the command:

 # crontab –e user1 

Where user1 is the name of the user that was granted permission to use the cron facility. By default, you could only edit your own cron file. Also, remember that you should never directly edit your cron file under the /var/spool/cron/crontabs directory. Always use the crontab command.