The manual Page
Version française
   
index | glossary | news | downloads | links ]
  Unix
Unix rights
manual pages
Unix commands
how to use vi
cron
rm
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  Cron and crontab

What is it used for?

Cron is a system used on Unix systems to execute commands at determined times. The main interest of this concerns administrative tasks: a server administrator would use cron to start every night a backup of his servers.

How does it work?

cron works thanks to the crond daemon. This program is started on a machine once for good and for every user. Users can parameter their own cron system with a small tool called crontab. The only problem is that crontab is the only standard program available to parameter crond and it is based on vi...

Crontab will give instructions to crond: when to execute a program and the rights with which it should be executed. Obviously, standard users cannot ask crond to start a program with the rights of another user, especially the super user (root); it would be too dangerous.

How to use crontab

To use it, it is essential to know how to use vi.

The first thing to do is to log on the machine as the user that should execute the commands. However, it is possible for the super user (root) to set another user for crond without explicitely log on the server as it by using the -u user_name option.

To start crontab and configure crond, we simply type: crontab -e at a command prompt. Then a vi screen appears. Parameters are set only when we leave vi.

Configuration of crond

crontab lines are of 2 kinds: there are cron commands, i.e. the system commands that crond should execute at a determined time, and environment variable definitions.

Comments are possible using a #.

cron commands

They are provided line by line. They have the following format:

time command

Commands are simply those the user would type if he had to execute them by himself.

A time can be divided into 5 fileds:

field possible values
minute 0-59
hour 0-23
day of the month 1-31
month 1-12
day of the week 0-7 (0 and 7 stand for sunday)

Fields are separated by spaces and if several elements of the same group must be provided, they are separated by commas (without space). A star (*) means: "for every element of this category". Example:

0,10,20,30,40,50 * 1 * 7 /usr/bin/foo

This line means: "execute /usr/bin/foo every 0, 10, 20... minutes of every hour of the first day of every month if this day is a monday, and this every year..." The combination may seem to be weird but it is extremely powerful. Basically, the principle is to parameter only relevant elements and to put a * everywhere else. To start foo every single minute, just use:

* * * * * /usr/bin/foo

The most frequently you can ask crond to execute a command is every minute.

Environment variables

Crontab lines can also be used to (re)set environment variable for executing cron commands. It is very easy to do:

VARIABLE=VALUE

This line will set VALUE to the the variable VARIABLE. It is possible to ask crond to reset environment variables set in .profile or .bashrc, except the LOGNAME variable for obvious security reasons. There is only one imperative: when setting a variable, never use spaces unless it is a string.

References

Les manual pages:

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.