Linux Step By Steps

TIMEKEEPING-CRON DAEMON

From: Mike Andrew June 2000

See Cron Noise to reduce /var/log/messages and mail output

See Personal Crontab


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


They are not particularly informative, and the last, is 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:

  1. Hand crafted jobs to run
  2. Jobs to run in the /etc/cron.d directory

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

ALL link to one or more programs in /etc/cron.d/lib (see footnote1 and footnote2)

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?

  1. So that you and others can document what the program does, and when you expect it to happen!
  2. 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 itself.


cron.d/lib

From: Ted Ozolins <ted1@telus.net>

Setting up several cron jobs under COL 3.1 I noticed that /etc/cron.d/lib no
longer exists, instead there is an /etc/cron.d/scripts.d.
I assumed that this is where my scripts should reside and everything works as
it should. 

Redhat

Redhat RH7.x systems and presumably their pre-decessors have no common link structure (no single repository), instead, messily, they install individual script binaries in separate folders. /etc/hourly eg