Anacron is a service in Linux, which can be used as a task scheduling tool. Commands can be executed periodically. It is also known as periodic command scheduler. Unlike the Linux croons Anacron does not assume the system is running continuously. So Anacron can be used in systems that are not running 24 hours in a day to control daily jobs weekly jobs and monthly jobs which are controlled by cron.

Configuration file: /etc/anacrontab. The Anacron checks the different jobs mentioned in its configuration file. A specific period in days, minutes, unique job identifier, and shell command are mentioned in Anacron the job entries.

Working of Anacron

For all the jobs, Anacron will check whether the job executed in the last x days, where x is the mentioned period for the specific job. Or Anacron will execute the command after waiting the exact time specified as the delay parameter in thecrontab. After executing the command Anacron record the data and time in the timestamp file for the job. So it understood when to execute the command again in the next time.

If thereisno more jobs to be run, Then also Anacron exits.

Anacron will consider different jobs whose identifier which is specified in the anacrontab matches the command line arguments. The various job arguments will be shell wildcard patterns. Specifying no arguments in the job is equal to specifying the parameter ‘*’. (allthe jobs will consider).

Unless the parameter -d is given Anacron forks to background when it begins, parent process occurs soon.
Unless the parameter -s or -n are given, Anacron will begin the job soon. The execution of different anacron jobs is independent.

If an Anacron job gives any output or standard error, the result will be mailed to the user who runs the Anacron. =

The information regarding the working of Anacron will be sent to syslogdunder priority notice.
All the Anacron error messages will be sent at priority error.

Active Anacron jobs mean the jobs that Anacron already decided to run and the jobs which currently being executed by Anacron. The Active Anacron jobs are ‘locked’, as a result the different copies of Anacron won’t execute them at the same time.

CronVs Anacron

Anacron is usually used as the cron for desktops and laptops. If you need to perform a background job automatically on a system which is not working 24/7 you can use anacron.

Example: If you scheduled a cron incrontabwhich needs to run a script everyday at 12 AM as a regular cron job and if your system is not on at that time the script won’t be executed. But if the same job is scheduled in Anacron then it will work when the system came back up.

CronConfiguration file:/etc/crontab
Anacron Configuration file : /etc/anacrontab.

####Anacron job is mentioned in the below format in the file /etc/anacrontab.

Period delay job identifier command
1.Period : This file will be a numeric value which mentions the number of days.

1 – daily
7 – weekly
30 – monthly
N – Any numeric value.

2. Delay:This field indicates the delay in minutes. If the delay is X then the Anacron will wait x minutes to execute the job after the machine starts.

3: Job:This file is for the time stamped file of the job. This is unique for each job. This file is available under the directory /var/spool/anacron. This file contains a single line which describes the last time the job executed.

adminz@adminz-Inspiron-3521 ~ $ ls -1 /var/spool/anacron/
cron.daily
cron.monthly
cron.weekly
adminz-Inspiron-3521 ~ # cat /var/spool/anacron/cron.daily
20150308

4.Command:This file contains the command or script to be executed.

Example for Anacron

cat/etc/anacrontab

7 30test.daily /bin/sh /home/anold/backup.sh

The example shows that the backup script runs everyday with a delay of 30 minutes.