Previous Table of Contents Next


Module 26
crontab (SV)

DESCRIPTION

Use the external crontab command to create a file containing control statements to be interpreted by the cron command. Each statement consists of a time pattern and a command. The cron program reads your crontab file and executes the commands at the time specified in the time patterns. The commands are usually executed by a Bourne shell (sh).

COMMAND FORMAT

Following is the general format of the crontab command.

     crontab    [ file ]
     crontab -e [ username ]
     crontab -l [ username ]
     crontab -r [ username ]

Options

The following options may be used to control how crontab functions.

-e Edit your crontab file using the editor defined by the EDITOR variable.
-r Removes your current crontab file. If username is specified then remove that user's crontab file. Only root can remove other users' crontab files.
-l List the contents of your current crontab file.

Arguments

The following list describes the arguments that may be passed to the crontab command.

file The name of the file you want to be used as your crontab file. The file is copied to a file named username in the system crontab directory.
If you do not specify a filename for crontab to read as input, crontab reads the standard input until you press Ctrl-D.

FURTHER DISCUSSION

The crontab command reads a file or the standard input to a directory that contains all users' crontab files. You can use crontab to remove your crontab file or display it. You cannot access other users' crontab files in the crontab directory.

Crontab File Format

The crontab file contains lines that consist of six fields separated by blanks (tabs or spaces). The first five fields are integers that specify the time the command is to be executed by cron. The following table defines the ranges and meanings of the first five fields.


Field Range Meaning

    1 0-59 Minutes
    2 0-23 Hours (Midnight is 0, 11 P.M. is 23)
    3 1-31 Day of Month
    4 1-12 Month of the Year
    5 0-6 Day of the Week (Sunday is 0, Saturday is 6)

Each field can contain an integer, a range, a list, or an asterisk (*). The integers specify exact times. The ranges specify a range of times. A list consists of integers and ranges. The asterisk (*) indicates all legal values (all possible times).

The following examples illustrate the format of typical crontab time patterns.


Time Pattern Description

0 0 * * 5 Run the command only on Thursday at midnight.
0 6 1,15 * 1 Run the command at 6 a.m. on the first and fifteenth of each month and every Monday.
00,30 7-20 * * * Run the command every 30 minutes from 7 a.m. to 8 p.m. every day.


NOTE:  
The day of the week and day of the month fields are interpreted separately if both are defined. To specify days to run by only one field, the other field must be set to an asterisk (*). In this case the asterisk means that no times are specified.



The sixth field contains the command that is executed by cron at the specified times. The command string is terminated by a new-line or a percent sign (%). Any text following the percent sign is sent to the command as standard input. The percent sign can be escaped by preceding it with a backslash (\%).

A line beginning with a # sign is a comment.

Each command in a crontab file is executed via the shell. The shell is invoked from your HOME directory (defined by $HOME variable). If you wish to have your (dot) .profile executed, you must specify so in the crontab file. For example,

     0 0 * * 1   . ./.profile ; databaseclnup

would cause the shell started by cron to execute your .profile, then execute the program databaseclnup. If you do not have your own .profile executed to set up your environment, cron supplies a default environment. Your HOME, LOGNAME, SHELL, and PATH variables are set. The HOME and LOGNAME are set appropriately for your login. SHELL is set to /bin/sh and PATH is set to :/bin:/usr/bin:/usr/lbin.


NOTE:  
Remember not to have any read commands in your .profile which prompt for input. This causes problems when the cron job executes.



Command Output

If you do not redirect the standard output and standard error of a command executed from your crontab file, the output is mailed to you.

Access

To use the crontab command you must have access permission. Your system administrator can make the crontab command available to all users, specific users, or no users. Two files are used to control who can and cannot access the command. The /usr/sbin/ cron.d/cron.allow file contains a list of all users who are allowed to use crontab. The /usr/sbin/cron.d/cron.deny file contains a list of all users who are denied access to crontab. If the cron.allow file exists but is empty, then all users can use the crontab command. If neither file exists, then no users other than the super-user can use crontab.

Displaying your crontab

If you have a crontab file in the system crontab area, you can list it by typing crontab -l. If you do not have a crontab file, crontab returns the following message:

     crontab: can't open your crontab file.

DIAGNOSTICS AND BUGS

The crontab command will complain about various syntax errors and time patterns not being in the valid range.


CAUTION:  
If you type crontab and press Return without a filename, the standard input is read as the new crontab entries. Therefore, if you inadvertently enter crontab this way and you want to exit without destroying the contents of your current crontab file, press the Del key. Do not press the Ctrl-D key; if you do, your crontab file will only contain what you have currently typed.



RELATED FILES

The crontab command uses the following files to execute your jobs as required.

/usr/sbin/cron.d The main directory for the cron process.
/usr/sbin/cron.d/log Accounting information for cron processing.
/usr/sbin/cron.d/crontab.allow A file containing a list of users allowed to use crontab.
/usr/sbin/cron.d/crontab.deny A file containing a list of users not allowed to use crontab.
/usr/spool/cron/crontabs Location of crontab text to be executed.

APPLICATIONS

You can use the crontab file to create a list of commands that are run every minute, hour, day, week, month, or year. You can perform nightly database queries or weekly database reorganizations using a crontab entry. Reports can be created and printed every morning automatically. Any task that is done on a regular basis is a candidate for crontab.

The most common use of the crontab command and cron facilities is system administration chores. File systems can be checked, database maintenance, accounting, and various other tasks can be performed by using crontab to create a table containing the necessary commands.

TYPICAL OPERATION

In this activity you use the crontab command to create a crontab file and then remove the crontab file. Begin crontab at the shell prompt.

1.  Type crontab and press Return.
2.  Type in the following line; press Return at the end of the line. Then press Ctrl-D at the beginning of the next line.
        cj> crontab
        0,5,10,15,20,25,30,35,40,45,50,55 * * * * echo "This is a test!"
        ^D

WARNING:  
Commands will be executed with /bin/sh.




If you are allowed to have a crontab, the command will not complain and your shell prompt returns. If you are not allowed to have a crontab file, crontab will display a message informing you of the condition. Contact your system administrator for access to using cron and crontab.
Wait for about 10 minutes, then continue to the next step.
3.  Type crontab -l and press Return to display the contents of your current crontab.
4.  Now remove your current crontab by typing crontab -r and pressing Return. You now have mail in your mailbox from the cron command running your cron entries. The mail messages are viewed later in Module 86.
5.  Turn to Module 4 to continue the learning sequence.


Previous Table of Contents Next