Using Cron

Cron is a handy utility available on (as far as I know) all Unix systems. It provides a mechanism for starting specified jobs at pre-determined times. So, for example, on my Linux laptop I have a cron job which runs every day which removes any files older than 7 days from the /tmp directory. Cron is excellent for dealing with these kinds of housekeeping tasks which otherwise are easily forgotten.

Cron is normally implemented as a daemon, ‘crond’, which is started automatically when the machine is booted. The cron daemon spends most of its life asleep, but once a minute it wakes up and checks if there are any jobs which need to be started at the current time. If there are, they are started and left to run. Any output from these programs will be captured and sent by email to the user that started the job, unless the output (and the error output) is explicitly redirected on the command line in the crontab file.

You may not have permission to use cron on your system, in which case you will need to (or ask your system administrator to) add your details to /etc/cron.allow and make sure they don’t appear in /etc/cron.deny.

Each user can set up their own personal crontab file, which specifies the schedule and the commands to run.

The crontab file is a plain text file with 6 whitespace delimited fields per line. The first 5 fields specify when to run the commands (which form the sixth field).

Here’s a sample line which removes old temporary files as mentioned in the example above:

0 0 * * * /usr/local/bin/find /tmp -depth -mtime +7 -exec /usr/local/bin/remove {} \;

This is what the numbers at the front of a crontab file mean:

Schedule Valid Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12
day of week 0-6 (Sunday is zero)

Use a star (*) to indicate all possible values.

So, the example above is executed at 0:00 (midnight) every day.

As you can see, the scheduling scheme is very flexible, allowing everything from once every minute (* * * * *) to once a year (10:00 on the third of January would be represented as 00 10 3 1 *) and almost everything in between.

An important point to bear in mind when using cron is the environment which is used to run the jobs. Cron uses ‘sh’, the Bourne shell to run the commands you specify. There is only a minimal PATH statement set up, which is why in the example above the full path name is specified for the find and remove commands.

It can be a good idea to prefix your cron commands with ‘. .profile’ to execute your .profile and set up all your default settings. One thing to be aware of here is that most interactive logins use either ksh (the Korn shell) or bash (the Bourne Again SHell), and there may be commands in your .profile which work fine in ksh or bash but which fail in sh.

The classic example is setting up environment variables. For example,

export PATH=$PATH:$HOME/bin

is valid syntax in ksh and bash, but not in sh. You need to export the variable as a separate step:

PATH=$PATH:$HOME/bin
export PATH

The crontab command is used to configure cron jobs.

crontab -l lists the current cron jobs on the screen
crontab -e edits the current cron jobs with the default editor (often vi)
crontab filename removes the current crontab file and replaces it with the contents of filename

Do a ‘man crontab’ for more details :-)

Do be careful when using the crontab command, because if you mistype the switches they can be interpreted as a non-existent filename which will effectively remove your existing crontab.

Leave a reply

You must be logged in to post a comment.