Thursday, January 26, 2012

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

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.
CronAnacron
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 serversIdeal for desktops and laptops
Use cron when a job has to be executed at a particular hour and minuteUse anacron when a job has to be executed irrespective of hour and minute