Scheduling Jobs

There will be situations in which you will want to run a program or script at a preset time, usually at a time when you’re not there. The Linux system provides a couple of ways to run a script at a preselected time: the at command and the cron table. Each method uses a different technique for scheduling when and how often to run scripts. The following sections describe each of these methods.

Scheduling a Job Using the at Command

The at command allows you to specify a time when the Linux system will run a script. The at command submits a job to a queue with directions on when the shell should run the job. The at daemon, atd, runs in the background and checks the job queue for jobs to run. Most Linux distributions start this daemon automatically at boot time.

The atd daemon checks a special directory on the system (usually /var/spool/at) for jobs submitted using the at command. By default, the atd daemon checks this directory every 60 seconds. When a job is present, the atd daemon checks the time the job is set to be run. If the time matches the current time, the atd daemon runs the job.

The following sections describe how to use the at command to submit jobs to run and how to manage jobs.

The at Command Format

The basic at command format is pretty simple:

at space open bracket hyphen f space filename close bracket space time.

By default, the at command submits input from the terminal to the queue. You can specify a file name used to read commands from using the -f parameter.

The time parameter specifies when you want the Linux system to run the job. You can get pretty creative with how you specify the time. The at command recognizes lots of different time formats:

  • A standard hour and minute, such as 10:15

  • An ~AM/~PM indicator, such as 10:15~PM

  • A specific named time, such as now, noon, midnight, or teatime (4~PM)

If you specify a time that has already past, the at command runs the job at that time on the next day.

In addition to specifying the time to run the job, you can also include a specific date, using a few different date formats:

  • A standard date format, such as MMDDYY, MM/DD/YY, or DD.MM.YY

  • A text date, such as Jul 4 or Dec 25, with or without the year

  • You can also specify a time increment:

    • Now + 25 minutes

    • 10:15~PM tomorrow

    • 10:15 + 7 days

When you use the at command, the job is submitted into a job queue. The job queue holds the jobs submitted by the at command for processing. There are 26 different job queues available for different priority levels. Job queues are referenced using lowercase letters, a through z.

The higher alphabetically the job queue, the lower the priority (higher nice value) the job will run under. By default, at jobs are submitted to the a job queue. If you want to run a job at a higher priority, you can specify a different queue letter using the -q parameter.

Retrieving Job Output

When the job runs on the Linux system, there’s no monitor associated with the job. Instead, the Linux system uses the email address of the user who submitted the job for any output messages. Any output destined to the terminal is mailed to the user via the mail system.

Here’s a simple example of using the at command to schedule a job to run:

An example of using the at command to schedule a job to run is shown.
Description

An example of using the at command to schedule a job to run is shown.
Description

The at command produces a warning message, indicating what shell the system uses to run the script, /bin/sh, along with the job number assigned to the job and the time the job is scheduled to run.

When the job completes, nothing appears on the monitor, but the system generates an email message. The email message shows the output generated by the script. If the script doesn’t produce any output, it won’t generate an email message, by default. You can change that by using the -m option in the at command. This generates an email message, indicating the job completed, even if the script doesn’t generate any output.

Listing Pending Jobs

The atq command allows you to view what jobs are pending on the system:

An output shows the jobs pending on the system using the a t q command.
Description

An output shows the jobs pending on the system using the a t q command.
Description

The job listing shows the job number, the date and time the system will run the job, and the job queue the job is stored in.

Removing Jobs

Once you know the information about what jobs are pending in the job queues, you can use the atrm command to remove a pending job:

An output shows removal of a pending job using the a t r m command.
Description

Just specify the job number you want to remove. You can only remove jobs that you submit for execution. You can’t remove jobs submitted by others.

Scheduling Recurring Programs

Using the at command to schedule a program or script to run at a preset time is great, but what if you need it to run at the same time every day, or once a week, or once a month? Instead of having to continually submit at jobs, you can use another feature of the Linux system.

The Linux system uses the cron program to allow you to schedule jobs that need to run on a regular basis. The cron program runs in the background and checks special tables, called cron tables, for jobs that are scheduled to run.

The cron Table

Each individual user account has a separate cron table for scheduling programs. The user cron table uses a special format for allowing you to specify when a job should be run. The format for the cron table is:

min space hour space day of month space month space day of week space command.

The cron table allows you to specify entries as specific values, ranges of values (such as 1–5) or as a wildcard character (the asterisk). For example, if you want to run a command at 10:15 on every day, you would use the cron table entry of:

15 space 10 space asterisk space asterisk space asterisk space command.

The wildcard character used in the dayofmonth, month, and dayofweek fields indicates that cron will execute the command every day of every month at 10:15. To specify a command to run at 4:15 PM every Monday, you would use the following:

15 space 16 space asterisk space asterisk space 1 space command.

You can specify the dayofweek entry as either a three-character text value (mon, tue, wed, thu, fri, sat, sun) or as a numeric value, with 0 being Sunday and 6 being Saturday.

Here’s another example: to execute a command at 12 noon on the first day of every month, you would use the following format:

0 0 space 12 space 1 space asterisk space asterisk space command.

The dayofmonth entry specifies a date value (1–31) for the month.

The command list must specify the full pathname of the command to run. You can add any command line parameters or redirection symbols you like, as a regular command line:

15 space 10 space asterisk space asterisk space asterisk space slash home slash sys admin slash test 4 dot s h space greater than space test 4 out.

The cron program runs the script using the user account that submitted the job. Thus, you must have the proper permissions to access the command and output files specified in the command listing.

Building the cron Table

Each system user can have their own cron table (including the root user) for running scheduled jobs. Linux provides the crontab command for handling the cron table. To list an existing cron table, use the -l parameter:

Line 1: dollar space crontab space hyphen l. Line 2: no crontab for rich. Line 3: dollar.

By default, each user’s cron table file doesn’t exist. To add entries to your cron table, use the -e parameter. When you do that, the crontab command starts a text editor with the existing cron table (or an empty file if it doesn’t yet exist).

cron Directories

When you create a script that has less precise execution time needs, it is easier to use one of the preconfigured cron script directories. There are four basic directories: hourly, daily, monthly, and weekly.

A script shows the usage of preconfigured cron script directories.
Description

Thus, if you have a script that needs to be run one time per day, just copy the script to the daily directory and cron executes it each day.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197