Cron Vs Anacron: How to Setup Anacron on Linux (With an Example)

by SathiyaMoorthy on May 10, 2011

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.

Anacrontab Format

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.

Anacron Example

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.

Cron Anacron
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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 11 comments… read them below or add one }

1 Leslie May 10, 2011 at 4:09 am

Nice article

2 tidalwaters May 10, 2011 at 5:03 am

fcron combines the features of both, plus more.

3 madhu May 10, 2011 at 1:36 pm

Will it bring the machine up and then run the job or it needs the machine to be up?

4 madhu May 10, 2011 at 9:25 pm

Please ignore my last comment that’s a stupid question.

5 Milap May 15, 2011 at 9:25 pm

It’s wonderful article…..nice work…..

6 jameslee May 23, 2011 at 9:54 am

Awesome..Thank you dear.

7 Jalal Hajigholamali November 2, 2012 at 6:53 am

Hi,

It’s very wonderful article…

8 Mohamed Abubakar January 23, 2013 at 6:30 am

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?

9 daixtr July 24, 2013 at 10:17 am

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).

10 dilip varma December 19, 2013 at 12:36 am

Hi,

great article

Thank you

11 neil Brosnan March 12, 2014 at 7:17 pm

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.

Leave a Comment

Previous post:

Next post: