Using cron to Manage Periodic Jobs





Using cron to Manage Periodic Jobs

Every Linux and Unix system includes a service called cron that enables users to automatically run specific commands or scripts periodically, according to user-defined timing options. This service offers a range of unique tasksperiodic backups, email status reports, home directory cleanups, and so forth.

There are two levels of cron process lists: the systemwide lists and the per-user lists. The systemwide cron process lists should be used only to manage system-oriented processes; for all personal jobs that need to be periodically run, the per-user cron lists should be used instead.

Adding Systemwide cron Processes

Systemwide cron processes are most often used for tasks such as periodic backups, log rotation, and system health monitoring. To create a systemwide cron process, you create a shell script that performs the task you want to accomplish and then, using the cp or mv commands, place it into one of several special directories in /etc:

  • Shell scripts copied to /etc/cron.hourly are run once every hour at one minute past the hour.

  • Shell scripts copied to /etc/cron.daily are run once every day at 4:02 a.m.

  • Shell scripts copied to /etc/cron.weekly are run once every week on Sunday at 4:22 a.m.

  • Shell scripts copied to /etc/cron.monthly are run once every month, on the first day of the month, at 4:42 a.m.

As is the case with all shell scripts you want to run, scripts that you copy to the /etc/cron.* folders must be marked as executable for them to work properly.

Monitoring with anacron

If you are wondering what happens to your daily shell scripts when your computer isn't running at 4:02 a.m., you needn't worry. Fedora Core 4 includes a service called anacron that monitors your cron tasks. When you first boot your Linux system, anacron runs those tasks that missed their last scheduled execution, ensuring that no cron job is ever missed.


Editing Per-User cron Processes

In addition to the systemwide list of cron tasks, which only the root user has permission to edit (because they reside in the /etc folder), cron provides a special way for individual users to manage a personal list of periodic tasks. This is done with the crontab command. To edit your personal lists of cron tasks, enter the crontab -e command at any command line:

[[email protected] ~]$ crontab -e

The vi editor takes over your terminal or console, pointing toward a special file that cron reads to perform the tasks you request. By editing and saving this file, you can cause cron to call any command or script for you on a periodic basis.

Each line in the file represents a separate task and must be a list of data fields of the following format:

min hour mday month wday command

The meaning of each of these fields is described in Figure. Each of them is meaningful: cron uses the values of every field on a specific line to construct the time(s) at which a task should be run.

Figure Fields in the crontab File

Field

Description and Format

min

The minute (from 0-60) at which this job should be run. Multiple values can be specified by separating them with commas (for example, 0,15,30,45), and a range of values can be specified to cause the job to run every minute in the range (for example, 15-40). Lists and ranges can also be mixed (for example, 0-15,30-45). An asterisk (*) indicates any minute.

hour

The hour (from 0-24) at which this job should be run. As was the case with min, lists, ranges, a combination of the two, or an asterisk may appear in this field.

mday

The day of the month (from 1-31) on which this job should be run. As was the case with min, lists, ranges, a combination of the two, or an asterisk may appear in this field.

month

The month of the year (from 1-12) in which this job should be run. As was the case with min, lists, ranges, a combination of the two, or an asterisk may appear in this field.

wday

The day of the week (from 0-7, both representing Sunday), on which this job should be run. As was the case with min, lists, ranges, a combination of the two, or an asterisk may appear in this field.


Enter each of your periodic tasks. After you finish, save the file and exit the vi editor; the new list of jobs is read by cron and carried out at the times you specify until the jobs are removed againmeaning when you enter crontab -e and delete them from the file.

Let's examine a few lines from a crontab file for illustration purposes:

* * * * * fetchmail
0 15 * * * mcopy ~/database.txt a:
1 0 1 1 * new_year_script

The first of these entries calls the fetchmail program described in Chapter 23, "Using the Network at the Command Line," once every minute of every day. In short, this line downloads the user's new mail once per minute.

The second of these entries copies a file called database.txt from the user's home directory to the floppy disk in drive a: every day at 3:00 p.m., presumably for backup purposes.

The final entry runs a shell script called new_year_script every New Year's Day at 12:01 a.m.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows