Anacron is the cron for desktops and laptops.
Anacron does not expect the system to be running 24 x 7 like a server.
When you want a background job to be executed automatically on a machine that is not running 24 x 7, you should use anacron.
For example, if you have a backup script scheduled everyday at 11 PM as a regular cron job, and if your laptop is not up at 11 PM, your backup job will not be executed.
However, if you have the same job scheduled in anacron, you can be sure that it will be executed once the laptop come back up.
Just like how cron has /etc/crontab, anacron has /etc/anacrontab.
/etc/anacrontab file has the anacron jobs mentioned in the following format.
period delay job-identifier command
Field 1 is Recurrence period: This is a numeric value that specifies the number of days.
- 1 – daily
- 7 – weekly
- 30 – monthly
- N – This can be any numeric value. N indicates number of days
Note: You can also use ‘@monthly’ for a job that needs to be executed monthly.
Field 2 is Delay: This indicates the delay in minutes. i.e X number of minutes anacron should wait before executing the job after the the machine starts.
Field 3 is Job identifier: It is the name for the job’s timestamp file. It should be unique for each job. This will be available as a file under the /var/spool/anacron directory. This file will contain a single line that indicates the last time when this job was executed.
# ls -1 /var/spool/anacron/ test.daily cron.daily cron.monthly cron.weekly # cat /var/spool/anacron/test.daily 20110507
Field 4 is command: Command or shell script that needs to be executed.
Just like shell scripts, comments inside anacrontab file starts with #
Note: For /etc/crontab file format, refer to our Linux Crontab: 15 Awesome Cron Job Examples article.
The following example executes the /home/sathiya/backup.sh script once in every 7 days.
On the day when the backup.sh job is supposed to executed, if the system is down for some reason, anacron will execute the backup.sh script 15 minutes after the system comes back up (without having to wait for another 7 days).
# cat /etc/anacrontab 7 15 test.daily /bin/sh /home/sathiya/backup.sh
START_HOURS_RANGE and RANDOM_DELAY
The above example indicates that the backup.sh script should be executed every day, with a delay of 15 mins. i.e When the laptop was started, executed it only after 15 minutes.
What happens when the laptop or desktop was not shutdown? When does the job gets executed? This is specified by the START_HOURS_RANGE environment variable in the /etc/anacrontab file.
By default this is set to 3-22 in the file. This indicates the time range from 3 a.m to 10 p.m.
# grep START /etc/anacrontab START_HOURS_RANGE=3-22
On top of the user defined delay specified in the 2nd field of the /etc/anacrontab file, anacron also randomly adds x number of minutes. The x is defined by the RANDOM_DELAY variable in the /etc/anacrontab file.
By default this is set to 45 in the file. This means that anacron will add x minutes (randomly picked from 0 and 45), and add this to the user defined delay.
# grep RANDOM /etc/anacrontab RANDOM_DELAY=45
Cron Vs Anacron
Cron and anacron has its own advantages and disadvantages. Depending on your requirement, use one of them.
|Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)||Minimum granularity is only in days|
|Cron job can be scheduled by any normal user ( if not restricted by super user )||Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )|
|Cron expects system to be running 24 x 7. If a job is scheduled, and system is down during that time, job is not executed.||Anacron doesn’t expect system to be running 24 x 7. If a job is scheduled, and system is down during that time, it start the jobs when the system comes back up.|
|Ideal for servers||Ideal for desktops and laptops|
|Use cron when a job has to be executed at a particular hour and minute||Use anacron when a job has to be executed irrespective of hour and minute|
Comments on this entry are closed.
fcron combines the features of both, plus more.
Will it bring the machine up and then run the job or it needs the machine to be up?
Please ignore my last comment that’s a stupid question.
It’s wonderful article…..nice work…..
Awesome..Thank you dear.
It’s very wonderful article…
In the anacron example, there is a discrepancy in the explanation. First it says the script backup.sh will be run once in 7 days. Then it is said in START_HOURS_RANGE that the script backup.sh will be run everyday.. So, which one is correct?
to reconcile the comment above about discrepancy, i think that yes anacron will attempt to run backup.sh everyday after powering on the laptop if it cannot detect that it has been run already. But if it detects that it has been run already, then it goes back to schedule the next run accordingly (although I don’t know whet that exactly is).
Thanks, I read your stuff and liked this one. I take RHCSA in two days and this
eliminated my ignorance of anacron.
I do wish linux gave better examples in man pages, like solaris always does.
A tidy and clear summary and I liked the comparison between cron and ancron.
Kind regards, Neil.
What are the workarounds to allow a user to run anacron jobs. I see the user-specific options, but I don’t know if the user creates a new anacrontab file, where to put it if needed, and how to ensure anacron will see the user’s jobs if not in the system anacrontab.
Its nice article nice job
How do I actually create an anacron job?
My /etc/anacrontab file is read only.
And don’t see any equivalent to crontab -e
Hi Ramesh, i must say u are doing a really great job with your articles. I have a study text with use in school but I can hardly understand it better than I do your articles. Thank you so much for your time and God bless all your efforts.
Thanks for your great article…
thanks sir for the great article…