Scheduling Cron Jobs

 < Day Day Up > 

You can schedule tasks for periodic execution using the cron daemon. The cron daemon starts specified tasks at specified times as defined in the crontab files. The cron daemon starts when the system boots and remains running as long as the system is up.

cron works by reading configuration files and acting on their contents. A typical configuration file would include the command to be run, the day and time to run the command, and the user name under which the command should be run. You can look at this scheduling of jobs as a way of issuing commands at a specific time. The configuration files are called crontab files.

crontab files are usually in the /var/spool/cron/crontabs directory. The Red Hat Linux system crontab files used in some of the upcoming examples were run are in /var/spool/cron.

The format of entries in the crontab file are as follows:

minute hour monthday month weekday user name command

where:

minute the minute of the hour, from 0-59.

hour the hour of the day, from 0-23.

monthday the day of the month, from 1-31.

month the month of the year, from 1-12.

weekday the day of the week, from 0 (Sunday) - 6 (Saturday).

user name the user who will run the command if necessary (not used in the example).

command specifies the command line or script file to run.

Be sure to check your Linux variant to ensure that crontab entries are in the same format and that the entries are in the same order.

You have many options in the crontab file for specifying the minute, hour, monthday, month, and weekday to perform a task. You could list one entry in a field and then a space, several entries in any field separated by a comma, two entries separated by a dash indicating a range, or an asterisk, which corresponds to all possible entries for the field.

Now, create the simplest imaginable example to see how cron works. Create a file called listing with the following contents in your home directory (/root on a Linux system):

 * * * * * ls -l / > /root/listing.out 

This file produces a long listing of the root directory every minute and send the output to listing.out in our home directory.

To "activate" or "install" the crontab, simply issue the crontab command and the name of the file. You could also specify a user name if you wanted to associate the file with a specific user. After installing the crontab file, issue crontab -l to view the installed crontab files. The following example shows the process of working with our crontab file called listing:

 # cat listing * * * * * ls -l / > /root/listing.out # crontab listing # crontab -l # DO NOT EDIT THIS FILE - edit the master and reinstall. # (listing installed on Thu Jan 2 11:34:25 2003) # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $) * * * * * ls -l / > /root/listing.out # ls -l | grep list -rw-r--r--    1 root     root          38 Jan   2 11:33 listing -rw-r--r--    1 root     root        1315 Jan   2 11:35 listing.out # cat listing.out total 164 drwxr-xr-x  2 root     root        1024 Oct 26 20:53 asodev_home drwxr-xr-x  2 root     root        1024 Jun 30   2002 backup drwxr-xr-x  2 root     root        1024 Jul   3   2002 bill drwxr-xr-x  2 root     root        2048 Oct 17 15:27 bin drwxr-xr-x  4 root     root        1024 Dec 24 00:05 boot drwxr-xr-x 20 root     root      116736 Dec 24 00:09 dev drwxr-xr-x 60 root     root        4096 Jan   1 23:30 etc drwxr-xr-x   7 root    root        4096 Nov 16 14:06 home drwxr-xr-x   2 root    root        1024 Jun 21   2001 initrd drwxr-xr-x   7 root    root        4096 Oct 17 15:28 lib drwxrwxrwx   2 linuxconnect linuxconnect   1024 Nov 9 11:15 linuxconnect drwx------   2 root    root       12288 Jun 11  2002 lost+found drwxr-xr-x   2 root    root        1024 Aug 27 00:49 misc drwxr-xr-x   4 root    root        1024 Dec 24 00:06 mnt drwxr-xr-x   2 root    root        1024 Aug 23  1999 opt dr-xr-xr-x  86 root    root           0 Dec 23 19:05 proc drwxr-x---  30 root    root        2048 Jan  2 11:35 root drwxr-xr-x   2 root    root        5120 Oct 17 18:24 sbin drwxrwxrwt  15 root    root        2048 Jan  2 11:02 tmp drwxr-xr-x  17 root    root        4096 Oct 17 13:05 usr drwxr-xr-x  22 root    root        1024 Nov  8 09:55 var # crontab -r # crontab -l no crontab for root # 

The first command shows the contents of the file listing that you created. Next, issue the crontab command to install listing. Next, issue crontab -l to see the file we have installed. Next is a long listing of our home directory, which shows that the file listing.out has indeed been produced. Then, cat the file to see its contents. Then, remove the installed file with crontab -r. Issuing crontab -l as the last command shows that no crontab files are installed for the user root.

System administrators get a lot of use out of cron by scheduling many time-and resource-consuming jobs during off hours. A typical task that is scheduled at night are backups.

The following hybrid example shows how a system administrator would schedule the full backup on day 6 and the incremental backup on other days. This is a hybrid example whereby you would substitute actual commands for the "full backup command" and "incremental backup commands":

 $ crontab -l 00 2 * * 6 full backup command 15 12 * * 1-5 incremental backup command 

The first entry is the full backup, and the second entry is the incremental backup. In the first entry, the minute is 00; in the second entry, the minute is 15. In the first entry, the hour is 2; in the second entry, the hour is 12. In both entries, the monthday and month are all legal values (*), meaning every monthday and month. In the first entry, the weekday is 6 for Saturday (0 is Sunday); in the second entry, the weekdays are 1-5, or Monday through Friday. The optional user name is not specified in either example. Finally, the backup command is provided:

minute

hour

monthday

month

weekday

user name

command

00

2

al

al

6

n/a

full backup

15

12

al

all

1-5

n/a

incremental


Another common use of cron for system administrators is to find core files on a daily or weekly basis. Core files are images of memory written to disk when a problem of some kind is encountered on the system. They can be written in a variety of places on the system depending on the problem. These files can sometimes be used to identify the source of the problem, so system administrators like to keep track of them. The following find command will be run once a week to find core files that have not been accessed in a week and writes the core file names to a file in the home directory of root:

 00 2 * * 6 find / -name core -atime 7 > /root/core.files 

The system administrator checks this file on Monday to see what core files have been produced over the last week. Similar to the backup example, this check is run every Saturday at 2:00 AM.

Users sometimes set up cron entries to invoke large compilations or large batch jobs during the night when the system is not heavily used. As long as your system administrator has not denied you access to running cron jobs, you are free to set up your own jobs. Your system administrator can list users who are permitted to use cron in the cron.allow file. If you are not listed in this file, the format of which is one user per line, you cannot run the crontab program. If cron.allow does not exist, cron.deny is checked to see whether any users have been explicitly denied access to crontab.

cron is very easy to use. Simply create your file, such I as had done with listing in the earlier example, and run crontab against the file. If you have jobs you would like to see run on a regular basis, such as running your make at night, cron is a useful tool.

     < Day Day Up > 


    Linux on HP Integrity Servers. A System Administrator's Guide
    Linux on HP Integrity Servers: A System Administrators Guide
    ISBN: 0131400002
    EAN: 2147483647
    Year: 2004
    Pages: 100

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net