Linux News
Filewatcher
Geeknews
Linuxnews.org
International
Linux Center
Click here to go to the Linux2000 homepage.

Tutorial of the Week

Have you ever wandered near your Linux box in the middle of the night, only to discover your hard disk working furiously? If you have, or just want a way for some task to occur at regular intervals, cron is the answer.

The cron daemon, crond , packaged with most Linux distributions, controls scheduling of regular occuring jobs. When started upon entry into multi-user mode, crond scans the directories /var/spool/cron/crontabs and /etc/cron.d and the file /etc/crontab for work to do. crond then awakens every minute, performs the work its record of jobs says it should do at that time, mails the output (by default) to the owning user, then sleeps until the beginning of the next minute.

How to Use cron

Individual users may use cron to automate tasks. Normally, all users may make use of cron. If the Superuser (root) has created /etc/cron.allow or /etc/cron.deny, then access to the cron facility depends on the contents of those files. If /etc/cron.allow exists, your user name must appear in it for you to use cron. If /etc/cron.deny exists but /etc/cron.allow does not, your user name must not appear in /etc/cron.deny, or cron will not work for you. To edit your cron settings, use the crontab command:

crontab -e

This will create a cron table, or "crontab file", which cron will read to find work. The crontab command looks first for the VISUAL environment variable, then for the EDITOR environment variable. It will use the editor named in those variables to provide editing of crontab files.

Why do we NOT edit the crontab file directly ?

The reason is that cron requires a specific format for its job entries. The crontab command performs syntax checking before allowing a newly edited crontab file to enter circulation. If the new crontab has a syntax error, crontab complains and asks if you want to edit again. To protect the crontab files, the crontab command makes root the owner of the crontab files.

--------------------------------- Listing 1 -----------------------------
$ crontab -l

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.6568 installed on Fri Aug 20 09:25:10 1999)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37
# vixie Exp $)
0 0,12 * * * /usr/bin/rdist -f /home/msk/distfile \
-o remove > /dev/null

What do the fields mean in a time-specific cron ?

The fields in a time specific cron command appear in this order:
minute (0 - 59 allowed)
hour (0 - 23 allowed)
day of month (1 - 31 allowed)
month (1 - 12 or names allowed)
day of week (0 - 7 or names allowed, both 0 and 7 being Sunday
and the command to run

The numerical fields also allow ranges of numbers, wild cards, lists and methods for running cron jobs every Nth interval, such as every third hour. The asterik character works as a wild card, representing every occurrence of the fields value.

Example:
The example below will run the ls command every minute of the noon hour on the first day of the month.

#  Minute   Hour   Day-Of-Month   Day-Of-Week
#  Command
#  *   12   1   *   *   /usr/bin/ls

I hope you find this useful, Enjoy!

Questions or want to submit a Quick Tutorial? Email me... my name is Darrell


hosting by blueznet.com design © 1998-2001 by L2K Designs