Show Table of Contents
Scheduling an anacron Job as
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:
- 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
- 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
- Start the
crond
service for the current session:~]#
systemctl start crond.service
- (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 configuringanacron
.
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
- Choose:
- Frequency of executing the job. For example, use
1
to specify every day or3
to specify once in 3 days. - The delay of executing the job. For example, use
0
to specify no delay or60
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 themy.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
- 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”.
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.