Red Hat Training

A Red Hat training course is available for Red Hat Linux

23.2. Scheduling a Recurring Asynchronous Job Using Anacron

Anacron, like cron, is a service that enables you to schedule running a task, often called a job, at regular times. However, anacron differs from cron in two ways:
  • If the system is not running at the scheduled time, an anacron job is postponed until the system is running;
  • An anacron job can run once per day at most.
Users specify anacron jobs in anacron table files, also called anacrontab files. These files are then read by the crond service, which executes the jobs.

23.2.1. Prerequisites for Anacrob Jobs

Before scheduling an anacron job:
  1. Verify that you have the cronie-anacron package installed:
    ~]# rpm -q cronie-anacron
    The cronie-anacron is likely to be installed already, because it is a sub-package of the cronie package. If it is not installed, use this command:
    ~]# yum install cronie-anacron
  2. The crond service is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:
    ~]# systemctl enable crond.service
  3. Start the crond service for the current session:
    ~]# systemctl start crond.service
  4. (optional) Configure anacron. For example, you can change:
    • shell to be used when executing jobs
    • the PATH environment variable
    • mail addressee if a job sends emails.
    See the anacrontab(5) manual page for information on configuring anacron.

Important

By default, the anacron configuration includes a condition that prevents it from running if the computer is not plugged in. This setting ensures that the battery is not drained by running anacron jobs.
If you want to allow anacron to run even if the computer runs on battery power, open the /etc/cron.hourly/0anacron file and comment out the following part:
# Do not run jobs when on battery power
online=1
for psupply in AC ADP0 ; do
    sysfile="/sys/class/power_supply/$psupply/online"

    if [ -f $sysfile ] ; then
        if [ `cat $sysfile 2>/dev/null`x = 1x ]; then
            online=1
            break
        else
            online=0
        fi
    fi
done

23.2.2. Scheduling an Anacron Job

Scheduling an anacron Job as root User

The root user uses the anacron table in /etc/anacrontab. Use the following procedure to schedule a job as root.

Procedure 23.1. Scheduling an anacron Job as root User

  1. Choose:
    • Frequency of executing the job. For example, use 1 to specify every day or 3 to specify once in 3 days.
    • The delay of executing the job. For example, use 0 to specify no delay or 60 to specify 1 hour of delay.
    • The job identifier, which will be used for logging. For example, use my.anacron.job to log the job with the my.anacron.job string.
    • The command to execute. For example, use /usr/local/bin/my-script.sh
    Combine the chosen values into the job specification. Here is an example specification:
    3 60 cron.daily /usr/local/bin/my-script.sh
  2. Add the resulting line to /etc/anacrontab.
The job will now run as scheduled.
For simple job examples, see the /etc/anacrontab file. For full reference on how to specify a job, see the anacrontab(5) manual page.

Scheduling Hourly, Daily, Weekly, and Monthly Jobs

You can schedule daily, weekly, and monthly jobs with anacron. See the section called “Scheduling Hourly, Daily, Weekly, and Monthly Jobs”.