6 Automating System Tasks

WARNING:

Oracle Linux 7 is now in Extended Support. See Oracle Linux Extended Support and Oracle Open Source Support Policies for more information.

Migrate applications and data to Oracle Linux 8 or Oracle Linux 9 as soon as possible.

This chapter describes how to configure the system to run tasks automatically within a specific period of time, at a specified time and date, or when the system is lightly loaded.

About Automating Tasks

You can use automated tasks to perform periodic backups, monitor the system, run custom scripts, as well as other administrative tasks.

The cron and anacron utilities enable you to schedule the execution of recurring tasks, referred to as jobs, according to a combination of the following: time, day of the month, month, day of the week, and week. With the cron command, you can schedule jobs to run as often as every minute. If the system is down when a job is scheduled, cron does not run the job when the system restarts.

The anacron command you to schedule a system job to run only once per day. However, if a scheduled job has not been run, that job runs when the system restarts. The anacron command is mainly intended for use on laptop computers.

You do not usually need to run cron and anacron directly. The crond daemon executes scheduled tasks on behalf of cron and it starts anacron once every hour. crond looks in /etc/crontab or in files in /etc/cron.d for system cron job definitions, and /var/spool/cron for cron job definitions belonging to users. crond checks each job definition to see whether it should run in the current minute. If a job is scheduled for execution, crond runs it as the owner of the job definition file or, for system cron jobs, the user specified in the job definition (if any).

crond runs the 0anacron script in the /etc/cron.hourly directory as root once per hour according to the schedule in /etc/cron.d/0hourly. If anacron is not already running and the system is connected to mains and not battery power, crond starts anacron.

anacron runs the scripts in the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories as root once per day, week or month, according to the job definitions that are scheduled in /etc/anacrontab.

Configuring cron Jobs

System cron jobs are defined in crontab-format files in /etc/crontab or in files in /etc/cron.d. A crontab file typically consists of definitions for the SHELL, PATH, MAILTO, and HOME variables for the environment in which the jobs are run, followed by the job definitions themselves. Comment lines start with a # character; job definitions are specified by using the following format:

                  minute  hour  day  month  day-of-week  user  command
               

Each of the fields that you can specify are defined as follows:

minute

Specify a value of 0-59.

hour

Specify a value of 0-23.

day

Specify a value of 1-31.

month

Specify a value of 1-12 or jan, feb,..., dec.

day-of-week

Specify a value of 0-7 (Sunday can be 0 or 7) or sun, mon,...,sat.

user

Specify the user running the command; or, you can specify an asterisk (*), which indicates the owner of the crontab file.

command

Specify the shell script or command to be run.

For the minute through day-of week fields, you can use the following special characters:

*

Specify an asterisk (*) for all of the valid values for the field.

-

Specify a dash (-) to indicate a range of integers, for example, 1-5.

,

Specify a list of values, separated by commas (,), for example, 0,2,4.

/

Specify a step value by using the forward slash (/), for example, /3 in the hour field. This entry is interpreted as every three hours.

For example, the following entry would run a command every five minutes on weekdays:

0-59/5  *  *  *  1-5  *  command
               

Run a command at one minute past midnight on the first day of the months April, June, September, and November:

1  0  1  4,6,9,11  *  *  command
               

The root user can add job definition entries to the /etc/crontab, or add crontab-format files to the /etc/cron.d directory.

Note:

If you add an executable job script to the /etc/cron.hourly directory, crond runs the script once every hour. Your script should check that it is not already running.

For more information, see the crontab(5) manual page.

Controlling Access to Running cron Jobs

If permitted, users other than root can configure cron tasks by using the crontab command. All user-defined crontab-format files are stored in the /var/spool/cron directory with the same name as the users that created them.

root can use the /etc/cron.allow and /etc/cron.deny files to restrict access to cron. crontab checks the access control files each time that a user tries to add or delete a cron job. If /etc/cron.allow exists, only users listed in it are allowed to use cron, and /etc/cron.deny is ignored. If /etc/cron.allow does not exist, users listed in /etc/cron.deny are not allowed to use cron. If neither file exists, only root can use cron. The format of both /etc/cron.allow and /etc/cron.deny is one user name on each line.

To create or edit a crontab file as a user, log in as that user and type the command crontab -e, which opens your crontab file in the vi editor (or the editor specified by the EDITOR or VISUAL environment variables). The file has the same format as /etc/crontab except that the user field is omitted. When you save changes to the file, these are written to the file /var/spool/cron/username . To list the contents of your crontab file, use the crontab -l command. To delete your crontab file, use the crontab -r command.

For more information, see the crontab(1) manual page.

Configuring anacron Jobs

System anacron jobs are defined in /etc/anacrontab, which contains definitions for the SHELL, PATH, MAILTO, RANDOM_DELAY, and START_HOURS_RANGE variables for the environment in which the jobs run, followed by the job definitions themselves. Comment lines start with a # character.

RANDOM_DELAY is the maximum number of random time in minutes that anacron adds to the delay parameter for a job. The default minimum delay is 6 minutes. The random offset is intended to prevent anacron overloading the system with too many jobs at the same time.

START_HOURS_RANGE is the time range of hours during the day when anacron can run scheduled jobs.

Job definitions are specified in the following format:

                  period  delay  job-id  command
               

The following is a description of the fields that may be included:

period

Frequency of job execution specified in days or as @daily, @weekly, or @monthly for once per day, week, or month.

delay

Number of minutes to wait before running a job.

job-id

Unique name for the job in log files.

command

The shell script or command to be run.

The following entries are taken from the default /etc/anacrontab file:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days  delay in minutes  job-identifier   command
1                5	             cron.daily       nice run-parts /etc/cron.daily
7                25	            cron.weekly      nice run-parts /etc/cron.weekly
@monthly         45	            cron.monthly     nice run-parts /etc/cron.monthly

By default, anacron runs jobs between 03:00 and 22:00 and randomly delays jobs by between 11 and 50 minutes. The job scripts in /etc/cron.daily, run anywhere between 03:11 and 03:50 every day if the system is running, or after the system is booted and the time is less than 22:00. The run-parts script sequentially executes every program within the directory specified as its argument.

Scripts in /etc/cron.weekly run once per week with a delay offset of between 31 and 70 minutes.

Scripts in /etc/cron.monthly run once per week with a delay offset of between 51 and 90 minutes.

For more information, see the anacron(8) and anacrontab(5) manual pages.

Running One-Time Tasks

You can use the at command to schedule a one-time task to run at a specified time, or the batch command to schedule a one-time task to run when the system load average drops below 0.8. The atd service must be running to use at or batch.

sudo systemctl is-active atd
active

at takes a time as its argument and reads the commands to be run from the standard input. For example, run the commands in the file atjob in 20 minutes time:

sudo at now + 20 minutes < ./atjob
job 1 at 2013-03-19 11:25

The atq command shows the at jobs that are queued to run:

sudo atq
1	2013-03-19 11:25 a root

The batch command also reads command from the standard input, but it does not run until the system load average drops below 0.8. For example:

sudo batch < batchjob
job 2 at 2013-03-19 11:31

To cancel one or more queued jobs, specify their job numbers to the atrm command, for example:

sudo atrm 1 2

For more information, see the at(1) manual page.

Changing the Behavior of Batch Jobs

The load average of a system, as displayed by the uptime and w commands, represents the average number of processes that are queued to run on the CPUs or CPU cores over a given time period. Typically, a system might not considered overloaded until the load average exceeds 0.8 times the number of CPUs or CPU cores. On such systems, you would usually want atd to be able to run batch jobs when the load average drops below the number of CPUs or CPU cores, rather than the default limit of 0.8. For example, on a system with 4 CPU cores, you could set the load-average limit above which atd will not run batch jobs to 3.2.

If you know that a batch job typically takes more than a minute to run, you can also change the minimum interval that atd waits between starting batch jobs. The default minimum interval is 60 seconds.

To change the load-average limit and minimum interval time for batch jobs:

  1. Edit the atd configuration file, /etc/sysconfig/atd, uncomment the line that defines the OPTS variable, and edit the line to specify the new load-average limit and minimum interval time, for example:

    OPTS="-b 100 -l 3"

    This example sets the minimum interval to 100 seconds and the load-average limit to 3.

  2. Restart the atd service:

    sudo systemctl restart atd
  3. Verify that the atd daemon is running with the new minimum interval and load-average limit:

    sudo systemctl status atd
    atd.service - Job spooling tools
       Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled)
       Active: active (running) since Mon 2014-04-28 15:37:04 BST; 2min 53s ago
     Main PID: 6731 (atd)
       CGroup: /system.slice/atd.service
               └─6731 /usr/sbin/atd -f -b 100 -l 3
    
    Apr 28 15:37:04 localhost.localdomain systemd[1]: Started Job spooling tools.

For more information, see the systemctl(1) and atd(8) manual pages.