Anacrons Vs Crons | Setting Up Anacrons on Linux

One of the biggest weakness of using Crontab is that it assumes that your system is always on. The task will never run if the system is not powered on at that time. In this article, we will discuss another way to schedule repeated tasks using Anacrons.

What are Anacrons?

Anacrons are used to run tasks periodically with a frequency defined in days. Unlike Crons, they assume that the system is not powered on the whole time.

If you set up a Cron using crontab then it assumes the availability of the system during all hours. For example, if you have a CRON setup once a day at 10 am then it assumes that the system will be powered on at 10 am. If in case the system is unavailable then the task will run at the next available schedule. However, if you use Anacrons, you can be assured that your task will run as soon as you power on your system the next time.

Cron Vs Anacron

CronAnacron
It is a daemon.It is not a daemon.
Appropriate for servers.Appropriate for personal systems.
Has the minimum frequency defined in minutes.Tasks can be run only on a daily basis.
Task doesn’t execute if the system is powered off.The task will execute as soon as the system is powered on.
Can be used by both normal and root users.Can only be used by root users.

The major difference between Cron and Anacron is that Cron works effectively on machines that will run continuously while the Anacron is intended for machines that will be powered off in a day or week.

Anacron Syntax

Anacrons are defined in /etc/anacrontab file.

There are 4 parameters in Anacrontab syntax:

period   delay   job-identifier   command

Field 1: Recurrence Period

A numeric value that specifies the number of days.
1 – Daily (You can also use @daily)
7 – Weekly (You can also use @weekly)
30 – Monthly (You can also use @monthly)
N – Any numeric Value (For example, 3 specifies every 3 days)

Field 2: Delay

It is the delay to be added after the system is powered on. Suppose it’s value is 10, then the Anacron will execute the task after 10 minutes the system is powered on.

Field 3: Job Name

The unique job name to identify the task.

Field 4: Command

Shell Script or Command to execute.

Example

For instance, let’s setup an Anacron to run daily.

Step 1

Login as root user. Anacrontab is not available for normal users, so ensure you have root access.

sudo su

Type in your root password. If you don’t have root access then contact your system administrator.

Step 2

Open your Anacrontab file (‘/etc/anacrontab’) with a text editor. The file will look like this:

/etc/anacrontab: configuration file for anacron
See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/root
LOGNAME=root
These replace cron's entries
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthly

Step 3

Add the following line in the Anacrontab file to run the task daily.

7       10      test.weekly      /bin/sh /home/vishesh/backup.sh

The above command will run the backup script once a week after waiting for 10 minutes after system start.

START_HOURS_RANGE and RANDOM_DELAY

You may add two more important variables in the Anacrontab File.

START_HOURS_RANGE

The hour range specifies the window in which the command will execute if the system was not shut down at all. By default, it has a value of 3-22. It means that the command will run between 3 am and 10 pm.

RANDOM_DELAY

The second parameter adds a random delay for executing the command. Anacrontab adds this delay to the delay provided while setting up the task. For example, if it’s value is 30, then it will add random delay of 0-30 minutes.

Conclusion

In conclusion, we discussed setting up Anacrons in Linux. Also, we also discussed the key differences between Crons and Anacrons.

You may also read the article on Setting Up Crontab here. Additionally, you may read more about Anacrons on Wikipedia.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *