[Previous] [Contents] [Index] [Next]

crontab

Schedule periodic background work (POSIX)

Syntax:

crontab [-d cron_dir] [-u user] [file]

crontab [-d cron_dir] [-e | -l | -r] [-u user]

Options:

-d cron_dir
The location of the crontab directory.
-e
Edit a user's crontab entry. If you don't specify the -u option, crontab edits your own entry. It uses vi unless the EDITOR environment variable names another editor.
-l
("el") List the crontab entry of the user. Without the -u option, the invoking user's entry is listed.
-r
Remove a user's crontab entry. Without the -u option, the invoking user's entry is removed.
-u user
Specify the user whose crontab is to be acted upon. When submitting a crontab, the new crontab entry replaces or creates that user's crontab. When removing (-r) or listing (-l) existing crontabs, this specifies which user's crontab to remove or list. Only root may use this option.
file
The pathname of a file that contains specifications for crontab entries (see the Description section for the format for these specifications). If no file is specified, crontab uses the standard input.

Description:

The crontab utility creates or replaces a user's crontab entry. You can input the new crontab entry by specifying a file that contains specifications for crontab entries. If you don't specify a file, the standard input is used.

Users may use crontab if their names appear in the /var/spool/cron/cron.allow file. If that file doesn't exist, the file /var/spool/cron/cron.deny is checked to determine whether the user should be denied access to crontab. If neither file exists, only the superuser is allowed to modify crontab entries. If cron.allow doesn't exist and cron.deny exists and is empty, then global usage is allowed. These permission files consist of one user name per line.

Each command you specify is executed from your home directory using the shell (/bin/sh). The cron utility supplies a default environment for the shell, defining HOME, LOGNAME, SHELL(=/bin/sh), PATH (=:/bin:/usr/bin) and TZ. Users who want to have their .profile executed must explicitly do so in their crontab entry.

A crontab entry consists of lines of six fields each. The fields are separated by blanks. The first five are integer patterns that specify the following:

Each of these patterns can be an asterisk (meaning all valid values), an element, a list of elements separated by commas, or a range separated by a hyphen (-).

An element is either a number or two numbers separated by a hyphen (meaning an inclusive range). You can specify days by two fields (day of the month and day of the week). If both are specified, both are adhered to.

As an example of specifying the two types of days, the following line:

    0 0 1,15 * 1

runs a command at 00:00h on the first and fifteenth of each month, as well as at 00:00h on every Monday. To specify days with only one field, the other field should be set to *. For example:

    0 0 * * 1

runs a command only on Mondays.

The sixth field of a line in a crontab entry is a string that is executed by the command interpreter at the specified times. A percent character in this field -- unless escaped by a backslash -- is translated to a newline character.

Only the first line (up to a % or end-of-line) of the command field is executed by the command interpreter.

Sample crontab entries

Invoke the calendar program each day one minute after midnight:

    1 0 * * * calendar -

Display the current time on the system console every 20 minutes:

    1,21,41 * * * * (echo -n " "; date; echo) > /dev/con1

Clean up the UUCP work directories each weekday at 5:30 am:

    30 5 * * 1-5 /usr/lib/uucp/uuclean

Remove any files under /tmp that haven't been modified in more than seven days:

    0 4 * * * find /tmp -mtime +7 -exec rm -f {} \;

Note:

By default, the output of your commands is mailed to you. You may wish to redirect the standard output and standard error of your commands to avoid getting the mailed output.


Examples:

List your own crontab entry:

   crontab -l

Files:

When an error occurs, a diagnostic message is written to the standard error.

The standard input may be read for the content of crontab files when they are being created (none of -e, -l or -r are specified) and no file is specified on the command line.

When a crontab file is being edited, the editor invoked by crontab inherits crontab's standard input, standard output, and standard error.

The following files relative to one of /var/spool/cron, or the directory named in the -d option are used by crontab:

cron.allow
If this file exists, it is read by crontab to determine the exclusive list of userids who are allowed to schedule cron jobs.
cron.deny
If cron.allow doesn't exist, crontab reads this file to determine a list of userids who are NOT allowed to schedule cron jobs.
crontabs/userid
The crontab utility may create, edit, list or remove this file.

Exit status:

0
Successful completion.
>0
An error occurred.

Caveats:

If you inadvertently enter the crontab utility with no argument, don't try to get out by typing Ctrl-D (end-of-file), since this would replace your crontab file with an empty file. In this case, you should exit by typing your interrupt character, which is typically Ctrl-C.

See also:

cron


[Previous] [Contents] [Index] [Next]