print PDF Ireland
Automated tasks

In Linux, tasks may be configured to run automatically during a given period of time. Red Hat Linux is pre-configured for the running of certain system tasks allowing to keep your system up to date. For example, the data base is up dated on a daily basis. A system administrator may use automated tasks to make copies of periodic backup, to monitor the system, to execute customized scripts, etc....

Configuration of a Cron task

The main configuration file of cron: /etc/crontab, contains the following lines:


# run-parts
01 **** root run-parts /etc/cron.hourly
02 4*** root run-parts /etc/cron.daily
22 4 **0 root run-parts /etc/cron.weekly
42 4 1 **root run-parts /etc/cron.monthly

The four first lines are variables used for configuring the environment in which the cron tasks are run. The value of the SHELL variable indicates to the system which shell environment it must use (in this example, bash shell), and the PATH variable defines the access path used for the commands run. The cron tasks result is sent by email to the user name defined by the MAILTO variable. If the MAILTO variable is an empty chain (MAILTO=""), no email will be sent. The HOME variable can be used for the setting of the directory to use for the running of commands or scripts.

Each line of the /etc/crontab file has the formate:

minute hour day month dayofweek command

* minute - whole number between 0 and 59
* hour - whole number between 0 and 23
*day - whole number between 1 and 31 (if the month is specified, the day must be valid)
* month - whole number between 1 and 12 (or abbreviation of the month name)
* dayofweek - whole number between 0 and 7, 0 or 7 representing Sunday (or abbreviation of the week day)
* command - The command to run. The command can be as ls/proc>>/tmp/proc or execution command of a customized script that you are the author.

For the values shown above, an asterisk (*) (*) may be used for indicating all the valid values. For example, an asterisk used for the month value means that an execution of the command every month (with respect of the restrictions of the other values).

A hyphen (-) placed between 2 whole numbers shows a range of whole numbers.
For example, 1-4 corresponds to whole figures: 1, 2, 3, 4.

A list of values separated by commas (,) corresponds to a list. For example: 3, 4, 6, 8 correspond to this 4 specific whole numbers.

The slash (/) may be used for specifying spaced out values. To change a whole number into a range, make it followed by /. For example: 0-59/2 allows to define a minute out of 2 in the minutes field. These spaced out values may also be used with an asterisk. For example, the value */3 may be used in the month field to change a month out of 3.

The lines beginning by a hash sign (#) correspond to comments and are not processed.

How to execute a script automatically on the dedicated server (in crond)?

First of all, you must make sure that the script works in shell mode, i.e. it is able to run from the command line. To do so, you must place in the first line #!/usr/local/bin/php if it's a php script, #!/usr/bin/perl if it's a perl script, or #!/bin/bash pour un script shell, or for a shell script, then use the chmod command 700 script.cgi on the script. If you have telnet/ssh access, you may test its execution with ./script.cgi.

You just need next to insert in the file , the script path and the run hours.

Example :

Put in place an automatic script to use mrtgs curve:

*/5 **** root /usr/local/mrtg-2/bin/mrtg /home/ovh/www/mrtg/mrtg-sys/mrtg-sys/ >/dev/null 2>/dev/null

/home/ovh/www/mrtg/mrtg-sys/mrtg-sys/ corresponds to the path to the run script.
*/5 **** corresponds to the run time.
Here the script will be launched every 5 minutes.