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
|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.
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.
For instance, let’s set up an Anacron to run daily.
Login as root user. Anacrontab is not available for normal users, so ensure you have root access.
Type in your root password. If you don’t have root access then contact your system administrator.
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.
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
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.
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.
The second parameter adds a random delay for executing the command.
In conclusion, we discussed setting up Anacrons in Linux. Also, we also discussed the key differences between Crons and
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.