Linux OS Service ‘anacron’

Service Name

anacron

Cron Vs Anacron

Similar to the cron service, the anacron service runs applications or scripts at specific times and dates. This allows for reliable unattended system operation – scheduled events are not missed if the system goes down – instead, they are run as soon as possible after their scheduled time. Candidates for an anacron action are often system administration activies, such as log rotation, that must be performed, even if late.

Unlike the cron service, anacron will not miss the execution of a scheduled job, even if the system is powered off. The activity will be performed when the system is next available. This makes anacron the preferred choice to initiate essential system administration tasks such as backup or disk space recovery.

Jobs are typically divided into three classes – daily, weekly and monthly. Control scripts are ordinary shell command lines and are placed into the /etc/cron.[class] directory. Anacron ensures that each job is run at the scheduled interval; there is no specific time guaranteed for these jobs to be run. Each time the job is run, the timestamp file /var/spool/anacron/cron.[class] is updated. This timestamp file is also the same mechanism that anacron uses to determine if the jobs need to be run.

Service Control

To manage the anacron service on future reboots and shutdowns, use the chkconfig tool:

# chkconfig anacron on
# chkconfig --list anacron
anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig anacron off

To control the anacron service immediately, use the service command:

# service anacron 
Usage: /etc/init.d/anacron {start|stop|restart|condrestart|status}

The following table describes each available command:

Command Description
start Invoke the anacron(8) daemon and supply the “-s” command-line switch.  This forces anacron(8) to execute all scheduled applications and script serially instead of starting them all in parallel.
stop Terminates the anacron(8) daemon.  Any active applications or scripts are not affected and continue to run to their completion.
restart Equivalent to a stop and then a start command.
condrestart Ignored if the anacron service is not currently running, else equivalent to a restart command.  Typically used by RPM upgrades to avoid starting the service unintentionally.
status Displays “anacron is stopped” unless the daemon is actively running scheduled applications or scripts.

How to check if anacron is running

Note that the non-standard behavior of the status command cannot be used to determine whether anacron has been run since the last system boot. Instead, use the timestamp of the files in the /var/spool/anacron directory. These files are touched when their corresponding event occurs:

# ls -lrt /var/spool/anacron 
total 12
-rw-------. 1 root root 9 Jul 27 03:12 cron.monthly
-rw-------. 1 root root 9 Aug 18 03:10 cron.weekly
-rw-------. 1 root root 9 Aug 18 04:09 cron.daily

The above output denotes the last anacron monthly event has occurred at 03:12 on 27 July.

Service Configuration

To obtain the anacron service, along with its daemon and related control files, install the anacron package:

# yum install cronie-anacron.x86_64

The events that anacron recognizes are defined in the /etc/anacrontab file. The default file is shown below:

# cat /etc/anacrontab 
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# Events are defined below
1 65 cron.daily run-parts /etc/cron.daily
7 70 cron.weekly run-parts /etc/cron.weekly
30 75 cron.monthly run-parts /etc/cron.monthly

Lines with syntax VAR=VALUE assign values to shell environment passed to the scripts and applications run when an event occurs. Each anacron daemon does not have hard-wired events. Instead, events are defined explicitly in the /etc/anacrontab file. By default, events are defined every one, seven and thirty days. The format of an event definition is:

[days]  [delay]  [job_id]  [command] [[arg] ...]

Here,
days : defines how frequently the [command] should be run, in days.
delay : the number of minutes on the event day before the [command] will be invoked.
job_id : identifies the anacron job in log files, and is used to name the timestamp file created in /var/spool/anacron

Related Post