System Administration Guide

Scheduling a Repetitive System Event (cron)

The following sections describe how to create, edit, display, and remove crontab files, as well as how to control access to them.

Inside a crontab File

The cron daemon schedules system events according to commands found within each crontab file. A crontab file consists of commands, one per line, that will be executed at regular intervals. The beginning of each line contains date and time information that tells the cron daemon when to execute the command.

For example, a crontab file named root is supplied during SunOS software installation. Its contents include these command lines:


0 20 * * 0,4 /etc/cron.d/logchecker
 5  4 * * 0   /usr/lib/newsyslog
15  3 * * *   /usr/lib/fs/nfs/nfsfind
 1  2 * * *  [ -x /usr/sbin/rtc ] && /usr/sbin/rtc -c > /dev/null 2>&1

The first command line instructs the system to run logchecker at 10 on Sundays and Thursdays nights. The second command line schedules the system to run newsyslog at 4:05 every Sunday morning. The third command line orders the system to execute nfsfind daily at 3:15 in the morning The fourth command line instructs the system to check for daylight savings time and makes corrections if necessary. If there is no RTC time zone nor an /etc/rtc_config file, this entry will do nothing.

For more information about the syntax of lines within a crontab file, see "Syntax of crontab File Entries".

The crontab files are stored in /usr/spool/cron/crontabs (or /var/spool/cron/crontabs). Several crontab files besides root are provided during SunOS software installation (see Table 59-2).

Table 59-2 Default crontab Files

crontab File

Function 

adm

Accounting 

lp

Printing 

root

General system functions and 

file system cleanup 

sys

Performance collection 

Other crontab files are named after the user accounts in which they are created, such as bob, mary, smith, or jones.

Besides the default crontab file, users can create crontab files to schedule their own system events. To access crontab files belonging to root or other users, superuser privileges are required.

Procedures explaining how to create, edit, display, and remove crontab files are described in "Commands for Scheduling System Events".

How the cron Daemon Handles Scheduling

The cron daemon handles the automatic scheduling of crontab commands. Its function is to check the /usr/spool/cron/crontab directory (or the /var/spool/cron/crontab directory, depending on your system configuration) for the presence of crontab files, normally every 15 minutes. It checks for new crontab files or changes to existing ones, reads the execution times listed within the files, and submits the commands for execution at the proper times.

In much the same way, the cron daemon controls the scheduling of at files, which are stored in the /usr/spool/cron/atjobs directory.

Syntax of crontab File Entries

A crontab file consists of commands, one per line, that execute automatically at the time specified by the first five fields at the beginning of each command line. These first five fields, described in Table 59-3, are separated by spaces. They indicate when the command will be executed.

Table 59-3 Values for crontab Time Fields

Time Field 

Values 

Minute  

0-59 

Hour 

0-23 

Day of month 

1-31 

Month 

1-12 

Day of week 

0-6 (0=Sunday) 

Follow these guidelines to use special characters in crontab time fields:

For example, the following sample crontab command entry displays a reminder in the user's console window at 4 p.m. on the first and fifteenth of every month.


0 16 1,15 * * echo Timesheets Due > /dev/console

Each command within a crontab file must consist of one line, even if it is very long, because crontab does not recognize extra carriage returns. For more detailed information about crontab entries and command options, refer to crontab(1).