3 Configuring anacron
Jobs
Use anacron
to schedule periodic tasks on systems with intermittent
rather than continuous uptime.
The anacron
utility schedules jobs to be run on a daily, weekly, or monthly
interval rather than specifying a particular day or time so that they aren't miss if the
system is offline. It was originally intended for use on laptop computers that are routinely
suspended or switched off, but it can also be used in an enterprise environment to schedule
tasks in persistent cloud instances, containers, and virtual machines that are routinely taken
offline to reduce power consumption and hosting costs when they aren't needed.
If anacron
isn't already running and the system is connected to mains and
not battery power, crond
starts anacron
automatically.
The crond
daemon runs the /etc/cron.hourly/0anacron
script as root
each hour according to the schedule in
/etc/cron.d/0hourly
. Then, based on the configuration settings in
/etc/anacrontab
, the 0anacron
script processes the
contents in the /etc/cron.daily
, /etc/cron.weekly
,
and /etc/cron.monthly
directories.
If a scheduled job hasn't been run because of system downtime, then that job runs when the system restarts.
System anacron
jobs are defined in /etc/anacrontab
as
follows:
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
The top of the file 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.
The bottom part of the file contains job definitions. Each job consists of entries that are spread across 4 columns under the following headings:
- period
-
The frequency of job execution specified in days or as
@daily
,@weekly
, or@monthly
for daily, weekly, or monthly. - delay
-
The number of minutes to wait before running a job.
- job-id
-
The unique name for the job in log files.
- command
-
The shell script or command to be run.
By default, anacron
runs jobs between 03:00 and 22:00 and delays jobs by
between 11 and 50 minutes. The job scripts in /etc/cron.daily
run between
03:11 and 03:50 every day if the system is running, or after the system is booted and the time
is earlier than 22:00. The run-parts
script sequentially runs every program
within the directory specified as its argument.
Scripts in /etc/cron.weekly
run weekly with a delay offset of between 31
and 70 minutes.
Scripts in /etc/cron.monthly
run monthly with a delay offset of between
51 and 90 minutes.
For more information, see the anacron(8)
and anacrontab(5)
manual pages.