Make your own free website on Tripod.com

Home

Understanding Cron the scheduler
Cron is the periodic event scheduler of your system. The following examples might give you an idea of its usefulness and necessity.

The point to catch onto here, is that cron doesn't care about the nature of what it is asked to run. It could be a script, or could, as easily be a binary. All that cron does, is issue the command line syntax asked of it, at regular intervals.

There are three man pages associated with cron

man cron
man crontab
man 5 crontab

They are not particularly informative, and the last, is to say the least, confusing. Here is what you need to know to make ANY program fire up at regular intervals (minutes, hours, daily, weekly, whatever).

The basic time tick of cron is one minute.

The all important file is /etc/crontab. There are TWO distinct sections to this file:

The latter is merely an extension to the first method and explained later. Both sections rely on the following consistent format

Crontab Format:

Minute | Hour | Day | Month | DayOfWeek | user | ThingToDo


'user' is the account name for the 'ThingToDo' to be run under. Exceptionally useful. You don't want every job running as root do you?
'ThingToDo' is as per any command line syntax. Typically, it invokes a script.
'Day' and 'Date' are easy to understand, but very difficult to read in their raw form

Thus

30 5 11 12 * root echo "hello"

On the 11th December at 5:30 in the morning, print hello on the console.

You can, if you wish, use English

30 5 * Dec Tuesday root echo "hello"

Say hello every Tuesday in December.

a more practical? example of this would be

30 * * * * root echo "hello"

say "hello" every 30 minutes

There are three constucts to any of the time and date fields

* Any value is valid (ie every hour)
9..5 Any value between 9 and 5 inclusive
*/5 every five minutes
9..5/2 every second hour between 9..5



Crontab Scripts:
This is the same construct as the first section but is organised so that the 'thing to do' is a series of scripts in /etc/cron.d. Specifically

/etc/cron.d/hourly
/etc/cron.d/daily
/etc/cron.d/weekly
/etc/cron.d/monthly

ALL link to one or more programs in /etc/cron.d/lib

The 'programs' in /etc/cron.d/lib are all shell scripts invoking other programs.

This method, this construction, is endemic in Linux. It is well worth your time understanding how the mechanism works because it is the core to /etc/rc.d/initd among many other parts of the Linux OS.

Step 1

Whatever it is you want to do, you place it in the ~/lib directory. This one directory is THE place where the actual work is done.

Most commonly, the thing you place in the ~/lib directory is a shell script. It is not mandatory, but even if your program is a binary, it is recomended to place a simple script in the ~/lib directory to execute it. Why?

So that you and others can document what the program does, and when you expect it to happen!
It is 'irregular' and disorderly to place binaries in folders other than /usr/bin /usr/sbin etc.

Step 2


Provide a link in one (or more) of the hourly, daily, weekly (etc) directories.

ln -s XXmystuff /etc/cron.d/lib/mystuff

XX refers to the order in which the scripts in that directory will be run. If your program 'mystuff' doesn't rely on on other things happening first, you can give it any value.




Restarting Cron

Is NOT necessary. In this rare instance in Linux, it is not necessary to restart the daemon after alterations. crond checks /etc/crontab every minute to ensure nothing has changed. If it has, it reloads


Resources:
linux.nf
Cron man pages