CRONTAB Quick Reference Guide

cron is a UNIX utility that allows tasks to be scheduled to run in the background at regular intervals via the cron daemon. These tasks are often termed as cron jobs under Solaris (amongst other UNIX and Linux destributions). The crontab is a file which contains the schedule of entries to be run and at specified time.

Commands

CommandComments
crontab -e Edit your crontab file, or create one if it doesn't already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

Syntax

The crontab is made of of 6 fields, the first 5 specify the date/time for the command/script to be scheduled and the sixth is the command to be run at that interval.

+------------- minute (0-59)
|  +----------- hour (0-23)
|  |  +--------- day of month (1-31)
|  |  |  +------- month (1-12)
|  |  |  |  +----- day of week (0-6 with 0=Sunday)
|  |  |  |  |
-  -  -  -  -
*  *  *  *  *  commands

Example

30  18  *  *  1-5  /daily-jobs.ksh
0   0   *  *  0,6  /my-weekend-script
0   2   1  *  *    /monthly-job

From the above we confirm that daily-jobs.ksh will run every Monday-Friday at 18:30, the my-wekend-script runs at midnight on a Saturday and Sunday, and the monthly-job run at 2am on the 1st of the month.

Enironment Variables

By default cron runs commands from the users home directory unless explicitely changed with the default shell /usr/bin/sh. The following default variables are used:

variableComments
export EDITOR=vi to specify the text editor to use to open the crontab file.
HOME the users home directory
LOGNAME users login id
PATH the users executable PATH (if none set defaults to /usr/bin:/usr/sbin:.)
SHELL defaults to the users login shell, otherwise uses /usr/bin/sh)

Important files

FileFunction
/etc/cron.d/cron.allow File containing usernames that have permission to submit cronjobs
/etc/cron.d/cron.deny File containing usernames that don't have permission to submit cronjobs
/var/cron/log Cron Log file
/var/spool/cron/crontabs Directory containing individual crontab files of all users.

Editing a crontab

Here id my preferred method of updating a CRONTAB, you may have your own or simply execute , whatever your method, this is just an example

  1. Login as the user you wish to modify the crontab for (i:e using su or if you are root crontab -e username
  2. Run crontab -l > file to copy the crontab to a file
  3. Edit the file using you favourite text editor to make the changes as required
  4. Tell cron of the new cron entries using crontab filename

Troubleshooting

cron logs

To determine if your cron jobs are running ok since they don't produce any output, you can check the /var/cron/log or /var/cron/olog (moves log file to /var/cron/olog if log file exceeds system ulimit)

The file looks something like this:

! *** cron started ***   pid = 260 Tue Jun  15 00:30:56 1999
>  CMD: [ -x /usr/sbin/rtc ] && /usr/sbin/rtc -c > /dev/null 2>&1
>  root 429 c Tue Jun 15 02:01:00 1999
<  root 429 c Tue Jun 15 02:01:00 1999 rc=1
>  CMD: /usr/sbin/logadm
>  root 440 c Tue Jun 15 03:10:00 1999
<  root 440 c Tue Jun 15 03:10:00 1999
>  CMD: [ -x /usr/lib/gss/gsscred_clean ] && /usr/lib/gss/gsscred_clean
>  root 452 c Tue Jun 15 03:30:00 1999
<  root 452 c Tue Jun 15 03:30:00 1999

It provides the following:

  • the CMD is the command that was run
  • next entry is the time the job started
  • next is the time the job finished
  • rc is the return code from the job

‘crontab -e' command returns a number and a ‘?'

Sometimes when you try to edit a crontab file using crontab -e command, it prints a number and a ‘?'. This is because by default the editor used by crontab -e command is the default solaris editor <code.ed and not vi editor. When ed starts up it prints the number of characters in the crontab file and does not have a prompt

To avoid this, simple com out of the ed editor by typing q. Now set the EDITOR environmental variable to vi and run crontab -e again :

% EDITOR=vi
% export EDITOR

Getting emails for each cron execution

By default each cron execution will result in an email being sent to the cron user. If you do not want these emails, append >/dev/null 2>&1 to the end of each line in your crontab. For example:

30  18  *  *  *  /scripts/myscript.ksh >/dev/null 2>&1

crontab header

If like me, I sometimes forget the field order of the crontab file and end up reading man pages. I now just add the following into my crontab file as comments and a gentle reminder:

# +------------- minute (0-59)
# |  +----------- hour (0-23)
# |  |  +--------- day of month (1-31)
# |  |  |  +------- month (1-12)
# |  |  |  |  +----- day of week (0-6 with 0=Sunday)
# |  |  |  |  |  commands