Anacron Linux service.

 

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.

 

 

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

 

 

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 #

 

 

Anacron Example

 

The following example executes the /home/sathish/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/sathish/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

 

Advertisements