sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Periodic Processes
module list

Cron

cron is a subsystem (named crond on linux) that relies on a daemon (also named crond on linux), that is used to schedule periodic processes. cron schedules these tasks according to a set of configuration files called crontabs, that specify what should be executed and when. The daemon wakes up periodically (usually every minute) to examine the crontabs and adjust its tasks according to changes found. It is thus unnecessary to restart (or send a HUP signal to) cron or its daemon if a crontab changes.

A crontab tells cron what commands to execute and when. There are two types of crontabs:

A crontab contains one or more crontab entries. Blank lines and lines that begin with # are ignored. A crontab entry is a single line containing 6 (or seven) fields separated by whitespace with this format

mins hrs days mons wdays [runas] cmd

Each numeric field (such as mins) can be a single number, a range or numbers separated by a dash, or a comma-separated list of numbers. It can also be *, which means 'all possible values'. Linux extensions also allow */5, which means each fifth period (each fifth minute, hour, etc.) or these abbreviations

       @reboot    :    Run once after reboot.
       @yearly    :    Run once a year, ie.  "0 0 1 1 *".
       @annually  :    Run once a year, ie.  "0 0 1 1 *".
       @monthly   :    Run once a month, ie. "0 0 1 * *".
       @weekly    :    Run once a week, ie.  "0 0 * * 0".
       @daily     :    Run once a day, ie.   "0 0 * * *".
       @hourly    :    Run once an hour, ie. "0 * * * *".

Note that whitespace is used as a delimiter between all the fields in crontab entries, but the cmd field may contain whitespace.

With RHEL6 and later, cron will also run jobs specified in the configuration file for anacron. anacron and its configuration file /etc/anacrontab will be discussed in a later section.

Environment

cron executes the commands in the crontab using the default system shell, connected to the user's HOME directory. A minimal default environment including PATH, SHELL, HOME and LOGNAME is defined. The PATH is a minimal system PATH, including the standard user binary directories. Commands should specify absolute paths.

Output from Commands

Be sure to redirect both standard output and standard error of your cron commands. If this is not done, cron will usually mail the output to you.

Specifying the day of execution

There are two syntaxes for specifying the day on which the command is to be executed: the day of the month and the weekday. This conflict is resolved as follows:

* 0 1-15 * 0,6 xxx

will be executed on each Saturday and Sunday and on the 1st through the 15th of each month.

Linux extensions

There are many linux extensions to the crontab format

Name = Value
will create an environment variable Name and give it the indicated Value before running each command. The environment variables HOME, SHELL, and PATH, which are given default values by cron, may be defined instead by the user in the crontab in this fashion. Note: the variable must be assigned completely. Don't attempt to do something fancy like 'add a directory to the current PATH'.

NOTE: For a reason that I do not yet understand, SElinux refuses to allow crond to connect to a network home directory on our linux systems before executing a cron job. This can be remedied by setting HOME=/ inside your crontab, and this setting is required for all personal crontabs. Files in your home directory can still be accessed using their standard permissions, however, and commands in your home directory can be executed. This only pertains to home directories located on a network file system. Local accounts are unaffected. 

Examples

[ Remember, user crontabs on our linux systems must specify HOME=/ ]

The /etc/cron.d files (system crontabs) on linux uses the extra field runas to indicate who the command should be run as. Normally, runas is root, but this field adds flexibility to these system crontabs. The following entry specifies to run the command /usr/local/bin/upd as root at one minute past 3AM every Wednesday, placing the output in /var/log/upd.log

01 3 * * 3 root /usr/local/bin/upd > /var/log/upd.log 2>&1

The system crontab on hills is not kept in /etc. Instead, it takes the place of the user crontab for root. Thus, there is not extra field. Below is a sample entry. This entry appends /var/adm/messages with the kernel diagnostic messages output since the last run of dmesg. (This is selected by the - option to dmesg.). Messages are added to the log file every 10 minutes of the day, at 5, 15, etc minutes past the hour.

05,15,25,35,45,55 * * * * /usr/sbin/dmesg - >>/var/adm/messages

The following entry is from the system crontabs on linux. In this crontab, the PATH and MAILTO environment variables are specified at the beginning of the crontab. 

PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

The command relies on this PATH variable to find its command. It runs the command at 42 minutes after 4AM on the first day of every month. The command, run-parts, is given a single argument, /etc/cron.monthly. If run-parts generates any standard output or standard error, it will be mailed to root.

42 4 1 * * root run-parts /etc/cron.monthly

Preview question: The term crontab refers both to the files cron uses as configuration files and to the program that is used to create those files. On some systems the crontab format is discussed either in the cron man page or in the man page for the crontab command. On others, the crontab format is in a separate man page. Investigate where to find the format of a crontab on the systems you know.

Prev This page was made entirely with free software on linux:  
Kompozer
and Openoffice.org     
Next

Copyright 2011 Greg Boyd - All Rights Reserved.
Document made with Kompozer