Scheduling Jobs in Red Hat Linux


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.

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

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