Red Hat Linux 9 Professional Secrets [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Red Hat Linux 9 Professional Secrets [Electronic resources] - نسخه متنی

Naba Barkakati

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Scheduling Jobs in Red Hat Linux



As a system administrator, you may need to run some programs automatically at regular intervals or execute one or more commands at a specified time in the future. Your Linux system includes the facilities to schedule jobs to run at any future date or time you want. You can set up the system to perform a task periodically or just once. Here are some typical tasks you can perform by scheduling jobs on your Linux system:



  • Back up the files in the middle of the night



  • Download large files in the early morning when the system is not busy



  • Send yourself messages as reminders of meetings



  • Analyze the system logs periodically and look for any abnormal activities



You can perform these tasks by using the

at command or the crontab facility of Red Hat Linux. The next few sections introduce these job-scheduling features of Red Hat Linux.


Scheduling One-Time Jobs


You can use the

at command to schedule the execution of one or more commands at a later time. The

atd daemon-a program designed to process jobs submitted using

at -executes the commands at the specified time, and mails the output to you.






Insider Insight

Before you try out the

at command, note that the following configuration files control, which users can schedule tasks using the

at command:




  • /etc/at.allow contains the names of the users who may submit jobs using the

    at command.



  • /etc/at.deny contains the names of users not allowed to submit jobs using the

    at command.

    If these files are not present, or if there is an empty

    /etc/at.deny file, any user can submit jobs using the

    at command. The default in Red Hat Linux is an empty

    /etc/at.deny file, so anyone can use the

    at command. If you do not want some users to use

    at , simply list those usernames in the

    /etc/at.deny file.



To use

at to schedule a one-time job for execution at a later time, follow these steps:



  1. Run the

    at command with the date or time when you want your commands executed. When you press Enter, the

    at > prompt appears, as follows:

    at 21:30
    at>

    This is the simplest way to indicate the time when you want to execute one or more commands-simply specify the time in a 24-hour format. In this case, you want to execute the commands at 9:30 p.m. tonight (or tomorrow, if it's already past 9:30 p.m.). You can, however, specify the execution time in many different ways (see Table 20-3 for examples).



  2. At the

    at > prompt, type the commands you want to execute as if typing input at the shell prompt. After each command, press Enter and continue with the next command. When you are finished entering the commands you want to execute, press Ctrl-D to indicate the end. Here is an example showing a single command:

    at> ps
    at> <EOT>
    job 1 at 2003-01-21 21:30

    After you press Ctrl-D, the

    at command responds with a job number and the date and time when the job will execute.










































    Table 20-3: Specifying the Time of Execution with the at Command


    Command


    When the Job Is Run


    at now


    Immediately


    at now + 15 minutes


    15 minutes from the current time


    at now + 4 hours


    4 hours from the current time


    at now + 7 days


    7 days from the current time


    at noon


    At noontime today (or tomorrow, if already past noon)


    at now next hour


    Exactly 60 minutes from now


    at now next day


    At the same time tomorrow


    at 17:00 tomorrow


    At 5:00

    p.m. tomorrow


    at 4:45pm


    At 4:45

    p.m. today (or tomorrow, if already past 4:45

    p.m. )


    at 3:00 Aug 16, 2003


    At 3:00

    a.m. on August 16, 2003




After you enter one or more jobs, you can view the current list of scheduled jobs with the

atq command:


atq
4 2003-04-19 03:00 a
5 2003-08-16 21:57 a
6 2003-10-26 16:45 a

The first field on each line shows the job number-the same number that the

at command displays when you submit the job. The next field shows the year, month, day, and time of execution. The last field shows the jobs pending in the queue named

a .

If you want to cancel a job, use the

atrm command to remove that job from the queue. When removing a job with the

atrm command, refer to the job by its number, as follows:

atrm 4

This deletes job 4 scheduled for 3:00 a.m. April 19, 2003.

When a job executes, the output is mailed to you. Type mail to read your

mail and to view the output from your jobs.

Note that

at is useful for scheduling jobs that need to run irregularly-for instance, if you want to clean up the

/var/logs directory because the

/var filesystem is over 85 percent full, you might use

at to archive the old log files and then delete them to reclaim the disk space.


Scheduling Recurring Jobs


Although

at is good for running commands at a specific time, it's not useful for running a program automatically at repeated intervals. You have to use

crontab to schedule such recurring jobs, also called cron jobs because they are processed by the

cron daemon (

crond ). You need to do this, for example, if you want to back up your files to tape at midnight every day.

Two files control who can schedule cron jobs using

crontab :



  • /etc/cron.allow contains the names of the users who may submit jobs using the

    crontab command.



  • /etc/cron.deny contains the names of users not allowed to submit jobs using the

    crontab command.



If the

/etc/cron.allow file exists, only users listed in this file can schedule cron jobs. If only the

/etc/cron.deny file exists, users listed in this file cannot schedule cron jobs. Neither file exists in Red Hat Linux, so any user can submit cron jobs.








Secret


You schedule recurring jobs by placing job information in a file with a specific format and submitting this file with the

crontab command. The cron daemon-

crond -checks the job information every minute and executes the recurring jobs at the specified times.

Any output from the job is mailed to the user who submits the job. (In the submitted job-information file, you may specify a different recipient for the mailed output.)

To submit a cron job, perform the following steps:



  1. Prepare a shell script (or an executable program in any programming language) that can perform the recurring task you want to perform. You can skip this step if you want to execute an existing program periodically.



  2. Prepare a text file with information about the times when you want the shell script or program (from Step 1) to execute. Submit this file using

    crontab . You can submit several recurring jobs with a single file. Each line with timing information about a job has a standard format with six fields-the first five specify when the job runs, and the sixth and subsequent fields constitute the actual command that runs. For example, here is a line that executes the

    myjob shell script in a user's home directory at 5 minutes past midnight each day:

      5 0 * * * $HOME/myjob

    Table 20-4 shows the meaning of the first five fields. Note that an asterisk (*) means all possible values for that field. Also, an entry in any of the first five fields can be a single number, a comma-separated list of numbers, a pair of numbers separated by a dash (indicating a range of numbers), or an asterisk.



























    Table 20-4: Specifying the Time of Execution in crontab Files


    Field Number


    Meaning of Field


    Acceptable Range of Values*


    1


    Minute


    0-59


    2


    Hour of the day


    0-23


    3


    Day of the month


    0-31


    4


    Month


    1-12 (1 means January, 2 means February, and so on) or the names of months using the first letters (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)


    5


    Day of the week


    0-6 (0 means Sunday, 1 means Monday, and so on) or the three-letter abbreviations of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat)


    * An asterisk in a field means all possible values for that field. For example, if an asterisk appears in the third field, the job is executed every day.



  3. Suppose the text file

    jobinfo (in the current directory) contains the job information. Submit this information to crontab with the following command:

      crontab jobinfo

    That's it! You should be set with the cron job. From now on, the cron job should run at regular intervals (as specified in the job information file), and you should receive mail messages with the output from the job.

    To verify that the job is indeed scheduled, type the following command:

    crontab -l

    The output of the

    crontab -l command shows the cron jobs currently installed in your name. To remove your cron jobs, type crontab -r.













If you log in as

root , you can also set up, examine, and remove cron jobs for any user. To set up cron jobs for a user, use this command:

crontab -u username filename

Here,

username is the user for whom you install the cron jobs, and

filename is the file that contains information about the jobs.

Use the following form of

crontab command to view the cron jobs for a user:

crontab -u username -l








Looking at the /etc/crontab File


The cron daemon (

crond ) also executes the cron jobs listed in the systemwide cron-job file

/etc/crontab . Here's the default

/etc/crontab file in Red Hat Linux (type cat /etc/crontab to view the file):

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
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

The first four lines set up several environment variables for the jobs listed in this file. Note that the

MAILTO environment variable specifies the user who receives the mail message with the output from the cron jobs in this file.

The line that begins with a

# is a comment line. The four lines following the run-parts comment execute the

run-parts shell script (located in the

/usr/bin directory) at various times with the name of a specific directory as argument. Each of the arguments to run-parts-

/etc/cron.hourly ,

/etc/cron.daily ,

/etc/cron.weekly , and

/etc/cron.monthly -are directories. Essentially,

run-parts executes all scripts located in the directory that you provide as an argument. This means that you can place scripts in these directories and get

crond to execute them at the appointed time. For example, a script placed in the

/etc/cron.daily gets executed at a certain time every day. Table 20-5 lists the directories and when they are executed. You have to look at the scripts in these directories to learn what gets executed at these periodic intervals.











To remove a user's cron jobs, use the following command:

crontab -u username -r
























Table 20-5: Script Directories for run-parts


Directory Name


Contents


/etc/cron.hourly


Scripts executed every hour


/etc/cron.daily


Scripts executed each day at 4:02

a.m.


/etc/cron.weekly


Scripts executed weekly on Sunday at 4:22

a.m.


/etc/cron.monthly


Scripts to be executed at 4:42

a.m. on the first day of each month



/ 341