≡ Menu

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

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
Add your comment

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

Comments on this entry are closed.

  • Leslie May 10, 2011, 4:09 am

    Nice article

  • tidalwaters May 10, 2011, 5:03 am

    fcron combines the features of both, plus more.

  • madhu May 10, 2011, 1:36 pm

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

  • madhu May 10, 2011, 9:25 pm

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

  • Milap May 15, 2011, 9:25 pm

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

  • jameslee May 23, 2011, 9:54 am

    Awesome..Thank you dear.

  • Jalal Hajigholamali November 2, 2012, 6:53 am

    Hi,

    It’s very wonderful article…

  • Mohamed Abubakar January 23, 2013, 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?

  • daixtr July 24, 2013, 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).

  • dilip varma December 19, 2013, 12:36 am

    Hi,

    great article

    Thank you

  • neil Brosnan March 12, 2014, 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.

  • von February 12, 2015, 4:49 pm

    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.

    –thanks

  • sagar April 19, 2015, 10:51 pm

    Thanks,

    Its nice article nice job

  • Matthew June 2, 2015, 10:25 pm

    How do I actually create an anacron job?

    My /etc/anacrontab file is read only.

    And don’t see any equivalent to crontab -e

    Thanks

  • Samuel July 1, 2015, 9:35 am

    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.

  • Sepahrad Salour July 3, 2015, 2:42 am

    Thanks for your great article…

  • Gaurav Gupta April 25, 2017, 5:35 am

    thanks sir for the great article…