Automating Tasks


 

Installed Documentation

cron man page overview of cron.
crontab man page The man page in section 1 contains an overview of the crontab file. The man page in section 5 contains the format for the file and some example entries.
/usr/share/doc/at-version/timespec contains more detailed information about the times that can be specified for cron jobs.
anacron man page description of anacron and its command line options.
anacrontab man page brief overview of the anacron configuration file.
/usr/share/doc/anacron-<version>/README describes Anacron and why it is useful.
at man page description of at and batch and their command line options.

 

Anacron

Anacron is a task scheduler similar to cron except that it does not require the system to run continuously. It can be used to run the daily, weekly, and monthly jobs usually run by cron.

To use the Anacron service, have the anacron RPM package installed and the anacron service must be running. To determine if the package is installed, use the rpm -q anacron command. To determine if the service is running, use the command /sbin/service anacron status.

 

Configuring Anacron Tasks

Anacron tasks are listed in the configuration file /etc/anacrontab. Each line in the configuration file corresponds to a task and has the format:

 period   delay   job-identifier  command

For each task, Anacron determines if the task has been executed within the period specified in the period field of the configuration file. If it has not been executed within the given period, Anacron executes the command specified in the command field after waiting the number of minutes specified in the delay field.

After the task is completed, Anacron records the date in a timestamp file in the /var/spool/anacron directory. Only the date is used (not the time), and the value of the job-identifier is used as the filename for the timestamp file.

Environment variables such as SHELL and PATH can be defined at the top of /etc/anacrontab as with the cron configuration file.

The default configuration file looks similar to the following:

 # /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# These entries are useful for a Red Hat Linux system.
1       5       cron.daily              run-parts /etc/cron.daily
7       10      cron.weekly             run-parts /etc/cron.weekly
30      15      cron.monthly            run-parts /etc/cron.monthly

Default anacrontab

As you can see in Figure 28-1, anacron for Red Hat Linux is configured to make sure the daily, weekly, and monthly cron tasks are run.

 

Starting and Stopping the Service

To start the anacron service, use the command /sbin/service" anacron start. To stop the service, use the command /sbin/service anacron stop. It is recommended that you start the service at boot time.

 

At and Batch

While cron and anacron are used to schedule recurring tasks, the at command is used to schedule a one-time task at a specific time. The batch command is used to schedule a one-time task to be executed when the systems load average drops below 0.8.

To use at or batch have the at RPM package installed, and the atd service must be running. To determine if the package is installed, use the rpm -q at command. To determine if the service is running, use the command /sbin/service" atd status.

 

Configuring At Jobs

To schedule a one-time job at a specific time, type the command at time, where time is the time to execute the command.

The argument time can be one of the following:

The time must be specified first, followed by the optional date. For more information about the time format, read the /usr/share/doc/at-<version>/timespec text file.

After typing the at command with the time argument, the at> prompt is displayed. Type the command to execute, press [Enter], and type Ctrl-D. More than one command can be specified by typing each command followed by the [Enter] key. After typing all the commands, press [Enter] to go to a blank line and type Ctrl-D. Alternatively, a shell script can be entered at the prompt, pressing [Enter] after each line in the script, and typing Ctrl-D on a blank line to exit. If a script is entered, the shell used is the shell set in the user's SHELL environment, the user's login shell, or /bin/sh (whichever is found first).

If the set of commands or script tries to display information to standard out, the output is emailed to the user.

Use the command atq to view pending jobs.

Usage of the at command can be restricted.

 

Configuring Batch Jobs

To execute a one-time task when the load average is below 0.8, use the batch command.

After typing the batch command, the at> prompt is displayed. Type the command to execute, press [Enter], and type Ctrl-D. More than one command can be specified by typing each command followed by the [Enter] key. After typing all the commands, press [Enter] to go to a blank line and type Ctrl-D. Alternatively, a shell script can be entered at the prompt, pressing [Enter] after each line in the script, and typing Ctrl-D on a blank line to exit. If a script is entered, the shell used is the shell set in the user's SHELL environment, the user's login shell, or /bin/sh (whichever is found first). As soon as the load average is below 0.8, the set of commands or script is executed.

If the set of commands or script tries to display information to standard out, the output is emailed to the user.

Use the command atq to view pending jobs.

Usage of the batch command can be restricted.

 

Viewing Pending Jobs

To view pending at and batch jobs, use the atq command. It displays a list of pending jobs, with each job on a line. Each line is in the format job number, date, hour, job class, and username. Users can only view their own jobs. If the root user executes the atq command, all jobs for all users are displayed.

 

Additional Command Line Options

Additional command line options for at and batch include:

 

OptionDescription
-f Read the commands or shell script from a file instead of specifying them at the prompt.
-m Send email to the user when the job has been completed.
-v Display the time that the job will be executed.

Table 28-1. at and batch Command Line Options

 

Controlling Access to At and Batch

The /etc/at.allow and /etc/at.deny files can be used to restrict access to the at and batch commands. The format of both access control files is one username on each line. Whitespace is not permitted in either file. The at daemon (atd) does not have to be restarted if the access control files are modified. The access control files are read each time a user tries to execute the at or batch commands.

The root user can always execute at and batch commands, regardless of the access control files.

If the file at.allow exists, only users listed in it are allowed to use at or batch, and the at.deny file is ignored.

If at.allow does not exist, all users listed in at.deny are not allowed to use at or batch.

 

Starting and Stopping the Service

To start the at service, use the command /sbin/service atd start. To stop the service, use the command /sbin/service atd stop. It is recommended that you start the service at boot time.

 

Automated Tasks

In Linux, tasks can be configured to run automatically within a specified period of time, on a specified date, or when the system load average is below a specified number. Red Hat Linux comes preconfigured to run important system tasks to keep the system updated. For example, the slocate database used by the locate command is updated daily. A system administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and more.

Red Hat Linux comes with four automated tasks utilities: cron, anacron, at, and batch.

 

Cron

Cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the time, day of the month, month, day of the week, and week.

Cron assumes that the system is on continuously. If the system is not on when a task is scheduled, it is not executed. To configure tasks based on time periods instead of exact times, refer to Section 28.2 Anacron. To schedule one-time tasks, refer to Section 28.3 At and Batch.

To use the cron service, have the vixie-cron RPM package installed, and the crond service must be running. To determine if the package is installed, use the rpm -q vixie-cron command. To determine if the service is running, use the command /sbin/service crond status.

 

Configuring Cron Tasks

The main configuration file for cron, /etc/crontab, contains the following lines:

 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 are variables used to configure the environment in which the cron tasks are run. The value of the SHELL variable tells the system which shell environment to use (in this example the bash shell), and the PATH variable defines the path used to execute commands. The output of the cron tasks are emailed to the username defined with the MAILTO variable. If the MAILTO variable is defined as an empty string ( MAILTO=""), email will not be sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.

Each line in the /etc/crontab file represents a task and has the format:

 minute   hour   day   month   dayofweek   command

For any of the above values, an asterisk (*) can be used to specify all valid values. For example, an asterisk for the month value means execute the command every month within the constraints of the other values.

A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.

A list of values separated by commas (,) specifies a list. For example, 3, 4, 6, 8 indicates those four specific integers.

The forward slash (/) can be used to specify step values. The value of an integer can be skipped within a range by following the range with /< integer>. For example, 0-59/2 can be used to define every other minute in the minute field. Step values can also be used with an asterisk. For instance, the value */3 can be used in the month field to run the task every third month.

Any lines that begin with a hash mark (#) are comments and are not processed.

As you can see from the /etc/crontab file, it uses the run-parts script to execute the scripts in the /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories on an hourly, daily, weekly, or monthly basis respectively. The files in these directories should be shell scripts.

If a cron task needs to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d directory. All files in this directory use the same syntax as /etc/crontab.

 # record the memory usage of the system every monday 
# at 3:30AM in the file /tmp/meminfo
30 3 * * mon cat /proc/meminfo >> /tmp/meminfo
# run custom script the first day of every month at 4:10AM
10 4 1 * * /root/scripts/backup.sh

Example 28-1. Crontab Examples

Users other than root can configure cron tasks by using the crontab utility. All user-defined crontabs are stored in the /var/spool/cron directory and are executed using the usernames of the users that created them. To create a crontab as a user, login as that user and type the command crontab -e to edit the user's crontab using the editor specified by the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab is stored according to username and written to the file /var/spool/cron/username.

The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron directory every minute for any changes. If any changes are found, they are loaded into memory. Thus, the daemon does not need to be restarted if a crontab file is changed.

 

Controlling Access to Cron

The /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. The format of both access control files is one username on each line. Whitespace is not permitted in either file. The cron daemon ( crond) does not have to be restarted if the access control files are modified. The access control files are read each time a user tries to add or delete a cron task.

The root user can always use cron, regardless of the usernames listed in the access control files.

If the file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is ignored.

If cron.allow does not exist, all users listed in cron.deny are not allowed to use cron.

 

Starting and Stopping the Service

To start the cron service, use the command /sbin/service" crond start. To stop the service, use the command /sbin/service crond stop. It is recommended that you start the service at boot time.


 

Home