Cron command syntax

A global cron file is provided with the UNIX system. This has been edited to include LMS housekeeping operations such as producing system logs, cleaning up the disk, clearing out error logs and some database related operations.

You can view the contents of the global cron file by logging in as root and typing:

# crontab -l

Cron command syntax

The cron process executes the jobs it contains at the times that are specified.  Each "cron job" is composed of six fields. Fields one to five contain clock information which specify when the command (given in the sixth field) should be executed.

Field

1

2

3

4

5

6

Function

Minute

Hour

Day of month

Month of year

Day of week

Command

Ranges

0-59

0-23

1-31

1-12

0-6

Begins with Sunday (0) and ends Saturday (6).

 

 

Clock information

Commands

In the sixth field of each cron line, commands can be added. The Capita convention is to use the /bin/su syntax to run the job as a specified user. For example to run a UNIX script called daliystatus.sc as the user called talis, the following syntax would be appropriate:

/bin/su – talis –c  “/users/talis/dailystatus.sc”

Note that the full pathname to the required script is being cited. This is good practice. You may wish to consider saving such commands in a separate file where they can be commented and pointed to from the cron line.  Jobs will be much easier to document, test and maintain and will run sequentially.
 

Output files

Reference to an output or report file can be made within the quotes surrounding the command. This will generate output just as if the command had been run directly from the UNIX command prompt. For example, to create a report from the dailystatus.sc script, the output could be re-directed thus:

/bin/su – talis –c  “/users/talis/dailystatus.sc >/scratch/dailystatus.out”

Note that the full pathname to the output target is used, in this case re-directing the file into the /scratch directory.

Redirecting output files
 

In addition to the output from the script, output can be created that relates to the running of the cron line itself.  For example:

/bin/su – talis –c  “/users/talis/dailystatus.sc >/scratch/dailystatus.out”  1>/var/tmp/dailystatus.cron  2>/var/tmp/dailystatus.err

The 1> is cron output and the 2> is error information.  Typically this is re-directed into the /var/tmp directory.  You may also see lines that force any cron error output into the same file as the cron output. This done using the syntax 2>&1 for example:

/bin/su – talis –c  “/users/talis/dailystatus.sc >/scratch/dailystatus.out”  1>/var/tmp/dailystatus.cron  2>&1

As quite often the cron output is not required, it can be re-directed into /dev/null. This effectively suppresses the output. For example:

/bin/su – talis –c  “/users/talis/dailystatus.sc >/scratch/dailystatus.out”  1>/dev/null  2>&1

If the cron outputs are not directed to a file or to /dev/null then such output will be sent to the UNIX mailbox for the root account. Use re-direction to limit the growth of this mailbox.

Example cron command line

The following line would run the dailystatus.sc script as a user called talis, at 8am Monday to Friday. It disregards the date. The script output would be placed into the /scratch directory and the cron output into /var/tmp whilst the cron error output is suppressed.

00   08   *   *   1-5  /bin/su – talis –c   “/users/talis/dailystatus.sc >/scratch/dailystatus.out”  1>/var/tmp/dailystatus.cronl  2>/dev/null