Using the cron Daemon

Using the cron Daemon

If you were a computer operating system and did not need sleep, you could back up users files at night. You could also rotate logs and delete temporary files while others sleep.

The cron daemon (also known by its command script, crond ) performs these tasks on an automated basis. When Linux starts, it runs crond as a background process. Every minute, it checks the appropriate configuration files to see if something needs to be run.

There are two groups of cron configuration files. One group is governed by a global configuration file, /etc/crontab . Another is based on those created by individual users with the crontab command.

Formatting cron

To understand how cron works, it s best to start with the basic cron configuration file, /etc/crontab . This file specifies several environment variables , including SHELL, PATH , and HOME. The following is a line-by-line analysis of this file:

 SHELL=/bin/bash 

The commands in this file are based on the bash shell.

 PATH=/sbin:/bin:/usr/sbin:/usr/bin 

When the commands in this file are located in the noted directories, the full directory path is not required. The PATH in /etc/crontab also determines the order in which the directories are searched. For example, if the flight command exists in both the /sbin and /usr/bin directories, cron runs the /sbin/flight command.

 MAILTO=root 

Every time crond actually does something, notification is mailed to the root user .

 HOME=/ 

The home directory associated with this /etc/crontab configuration file is the root ( / ) directory.

 # run-parts 

The run-parts command runs every script file in the specified directory. This allows you to organize the scripts that you need to run on a periodic basis.

 01 * * * * root run-parts /etc/cron.hourly 

This command runs every script in the /etc/cron.hourly directory at one minute past every hour , every day.

 02 4 * * * root run-parts /etc/cron.daily 

This command runs every script in the /etc/cron.daily directory at 4:02 a.m ., every day.

 22 4 * * 0 root run-parts /etc/cron.weekly 

This command runs every script in the /etc/cron.weekly directory at 4:22 a.m every Sunday.

 42 4 1 * * root run-parts /etc/cron.monthly 

This command runs every script in the /etc/cron.monthly directory at 4:42 a.m on the first day of every month

The numbers and asterisks in the commands may seem cryptic. Let s take a closer look.

The Syntax of cron

To use cron effectively, you need to understand the time and date fields on the left side of each command in a cron file. Table 13.1 shows the five fields, from left to right.

Table 13.1: cron Fields

Field

Allowable Range

Minute

0 “59

Hour

0 “23, where 0 is midnight and 20 is 8 p.m. .

Day

1 “31

Month

1 “12

Day of week

0 “7, where 0 and 7 both represent Sunday.

An asterisk in any field is a wildcard. For example, if the first field contains an asterisk, that particular job is run every allowable minute.

If you want to specify a range such as every hour between 8:00 a.m and 4:00 p.m. , set the second field to 8-16 . Alternatively, you can run a job every other day by setting the third field to */2 . As you can see, once you know each of the five fields (minute, hour, day, month, day of week), there is nothing cryptic about any of the cron command fields.

Standard cron Jobs

When you install Red Hat Linux, the standard configuration includes a set of cron jobs. This configuration allows you to organize cron jobs on an hourly, daily, monthly, and weekly basis. Each of these categories includes its own directory: /etc/cron.hourly , /etc/cron.daily , /etc/cron.weekly , and /etc/cron.monthly .

Take a look at several standard cron jobs that are run on a daily basis:

logrotate Rotates logs periodically. For example, Red Hat Linux rotates five weeks of logs, and the /var/log/messages entries from the previous week are kept in the /var/log/messages.1 file.

slocate .cron Refreshes the database associated with the locate command. By default, the database updates exclude directories that are networked from other computers, as well as several temporary directories.

tmpwatch Deletes files in the /tmp and /var/tmp directories. By default, files in these directories are deleted if they haven t been accessed in 240 and 720 hours, respectively.

User cron Jobs

Linux users may want to schedule their own cron jobs. For example, someone may want to manage a database in the middle of the night. As long as that user is not on the /etc/cron.deny list (described later in this chapter), that user can start his or her own cron file by using the crontab -e command.

Note  

While crontab uses the vi editor by default, you can set it to use another editor. For example, if you want to use emacs to edit your cron file, run the export EDITOR=emacs command.

For example, assume you ve configured a script named goodback to back up all the files in your home directory. You want to run goodback every Sunday morning at 1:36 a.m . Assume your username is ez, and your script is in your a default home directory ( /home/ez ). Log in as ez, and then run crontab -e . Assuming you re using the default vi editor, type i to enter insert mode, and type the following line:

 36 1 * * 0     /home/ez/goodback 

Once you ve saved the file, you can check the contents with the crontab -l command. All user cron files are stored in the /var/spool/cron directory and are accessible by default to the owner and the root user.

Note  

If you re creating a cron file, you should also assign the SHELL, PATH, and HOME variables. It s also a good idea to set the MAILTO variable, as it can notify you whenever cron actually runs one of your jobs. For guidance, see the earlier section, "Formatting cron ," which detailed the default /etc/crontab file.

start sidebar
Script Management

When you run a cron job, you re running a script. This is an executable file with commands that you could otherwise run at the command-line interface. You can also put any command that you use frequently into a file by using a text editor. Save the file and then use the chmod +x script1 command to make it executable. Assuming, for example, the file is in the /path/to directory, you run it at any time by typing the /path/to/ script1 command. If you have several commands that you normally run at the same time, you could expand that one-line file to include several commands. This is a great timesaver.

Saving your scripts to a directory in your PATH is even more efficient. For example, say your username is tb. Run the echo $PATH command. You should see the /home/tb/bin directory in your PATH. If you save scripts such as script1 to /home/tb/bin , all you d need to do to run that script is run the script1 command.

Just remember, if you re going to run script1 as a cron job, you need to add the appropriate directory to the PATH as described earlier.

end sidebar
 

cron Security

By default, cron tools are available to all users. You can limit access to cron by creating /etc/cron .allow and/or /etc/cron.deny files. There are three possible scenarios for these files:

  • Neither of these files exists, which means every user is allowed access to cron .

  • Users listed in /etc/cron.allow are the only ones allowed access to cron tools. If you also have an /etc/cron.deny file, it is ignored.

  • Users listed in /etc/cron.deny are not allowed to use cron tools. This assumes /etc/cron.allow does not exist.

 


Mastering Red Hat Linux 9
Building Tablet PC Applications (Pro-Developer)
ISBN: 078214179X
EAN: 2147483647
Year: 2005
Pages: 220

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