Module 26 crontab (SV)

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.


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net