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.

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”.