Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

 Purchase  Copyright © 2002 Paul Sheer. Click here for copying permissions.  Home 

next up previous contents
Next: 38. postgres SQL Server Up: rute Previous: 36. httpd   Contents

Subsections

37. crond and atd

crond and atd are two very simple and important services that everyone should be familiar with. crond does the job of running commands periodically (daily, weekly), and atd's main feature is to run a command once at some future time.

These two services are so basic that we are not going to detail their package contents and invocation.

37.1 /etc/crontab Configuration File

The /etc/crontab file dictates a list of periodic jobs to be run--like updating the locate (see page [*]) and whatis (see page [*]) databases, rotating logs (see Section 21.4.9), and possibly performing backup tasks. If anything needs to be done periodically, you can schedule that job in this file. /etc/crontab is read by crond on startup. crond will already be running on all but the most broken of UNIX systems.

After modifying /etc/crontab, you should restart crond with /etc/rc.d/init.d/crond restart (or /etc/init.d/crond restart, or /etc/init.d/cron restart).

/etc/crontab consists of single line definitions for the time of the day/week/month at which a particular command should be run. Each line has the form,

 
<time> <user> <executable>

where <time> is a time pattern that the current time must match for the command to be executed, <user> tells under what user the command is to be executed, and <executable> is the command to be run.

The time pattern gives the minute, hour, day of the month, month, and weekday that the current time is compared. The comparison is done at the start of every single minute. If crond gets a match, it will execute the command. A simple time pattern is as follows.

 
50 13 2 9 6 root /usr/bin/play /etc/theetone.wav

which will playen WAV Sat Sep 2 13:50:00 every year, and

 
50 13 2 * * root /usr/bin/play /etc/theetone.wav

will play it at 13:50:00 on the 2nd of every month, and

 
50 13 * * 6 root /usr/bin/play /etc/theetone.wav

will do the same on every Saturday. Further,

 
50 13,14 * * 5,6,7 root /usr/bin/play /etc/theetone.wav

will play at 13:50:00 and at 14:50:00 on Friday, Saturday, and Sunday, while

 
*/10 * * * 6 root /usr/bin/play /etc/theetone.wav

will play every 10 minutes the whole of Saturday. The / is a special notation meaning ``in steps of''.

Note that in the above examples, the play command is executed as root.

The following is an actual /etc/crontab file:

 
 
 
 
5 
 
 
 
 
10 
 
 
 
 
# Environment variables first
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
 
# Time specs
30 20 * * *  root  /etc/cron-alarm.sh
35 19 * * *  root  /etc/cron-alarm.sh
58 18 * * *  root  /etc/cron-alarm.sh
01 *  * * *  root  run-parts /etc/cron.hourly
02 4  * * *  root  run-parts /etc/cron.daily
22 4  * * 0  root  run-parts /etc/cron.weekly
42 4  1 * *  root  run-parts /etc/cron.monthly

Note that the # character is used for comments as usual. crond also allows you to specify environment variables under which commands are to be run.

Your time additions should come like mine have, to remind me of the last three Metro trains of the day.

The last four entries are vendor supplied. The run-parts command is a simple script to run all the commands listed under /etc/cron.hourly, /etc/cron.daily, etc. Hence, if you have a script that needs to be run every day but not at a specific time, you needn't edit your crontab file: rather just place the script with the others in /etc/cron.<interval>.

My own /etc/cron.daily/ directory contains:

 
 
 
 
5 
 
 
 
 
10 
total 14
drwxr-xr-x    2 root     root         1024 Sep  2 13:22 .
drwxr-xr-x   59 root     root         6144 Aug 31 13:11 ..
-rwxr-xr-x    1 root     root          140 Aug 13 16:16 backup
-rwxr-xr-x    1 root     root           51 Jun 16  1999 logrotate
-rwxr-xr-x    1 root     root          390 Sep 14  1999 makewhatis.cron
-rwxr-xr-x    1 root     root          459 Mar 25  1999 radiusd.cron.daily
-rwxr-xr-x    1 root     root           99 Jul 23 23:48 slocate.cron
-rwxr-xr-x    1 root     root          103 Sep 25  1999 tetex.cron
-rwxr-xr-x    1 root     root          104 Aug 30  1999 tmpwatch

It is advisable to go through each of these now to see what your system is doing to itself behind your back.

37.2 The at Command

at will execute a command at some future time, and only once. I suppose it is essential to know, although I never used it myself until writing this chapter. at is the front end to the atd daemon which, like crond will almost definitely be running.

Try our wave file example, remembering to press Ctrl-D to get the <EOT> (End Of Text):

 
 
 
 
5 
[root@cericon /etc]# at 14:19
at> /usr/bin/play /etc/theetone.wav
at> <EOT>
warning: commands will be executed using /bin/sh
job 3 at 2000-09-02 14:19

You can type atq to get a list of current jobs:

 
3       2000-09-02 14:19 a

a means is the queue name, 3 is the job number, and 2000-09-02 14:19 is the scheduled time of execution. While play is executing, atq will display:

 
3       2000-09-02 14:19 =

The at and atd man pages contain additional information.

Note that atd should generally be disabled for security.

37.3 Other cron Packages

There are many crond implementations. Some have more flexible config files, and others have functionality cope with job schedules that run when the machine is typically switched off (like home PCs). Your distribution may have chosen one of these packages instead.


next up previous contents
Next: 38. postgres SQL Server Up: rute Previous: 36. httpd   Contents