UnixServerAdmin

Server Administration & Management

How to Setup Anacron in CentOS/RHEL 6.x

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.

CentOS/RHEL 6.x has changed how the default system maintenance cronjobs are scheduled. These are the cron jobs responsible for things like rotating logs and indexing files on the filesystem. That is routine jobs that are best scheduled to run at off peak times when the server is not busy doing more important things like serving money making websites. First, a look at how the jobs are scheduled. We just follow the trail:

* Previously, in CentOS/RHEL 5.x system maintenance cronjobs were scheduled using the /etc/crontab file. This is no longer the case in CentOS 6.x. /etc/crontab is empty by default.
* Anacron is installed by default and is responsible for maintenance cron jobs. Anacron was originally created for running cron jobs on systems that aren’t switched on 24/7 i.e. desktop or workstation PCs. So why Redhat chose to include it by default in an enterprise operating system designed for servers that are always on is anyone’s guess.
* Anacron is run by crond via the 0anacron file in /etc/cron.hourly. Anacron does NOT run as it’s own daemon.
* Anacron is configured using /etc/anacrontab and executes the commands in the cron.daily/weekly/monthly directories:
* Just like how cron has /etc/crontab, anacron has /etc/anacrontab, /etc/anacrontab file has the anacron jobs mentioned in the following format.

————————————————————————————–
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=20-22
 
#period in days   delay in minutes   job-identifier       command
1                   5                cron.daily      nice run-parts /etc/cron.daily
7                   25               cron.weekly     nice run-parts /etc/cron.weekly
@monthly        45             cron.monthly    nice run-parts /etc/cron.monthly
————————————————————————————–

$–> 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
@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.

$–> Field 4 is command: Command or shell script that needs to be executed.

START_HOURS_RANGE :- Interval, when scheduled jobs can be run, in hours, In case the time interval is missed, for example due to a power failure, the scheduled jobs are not executed that day.

RANDOM_DELAY :- maximum number of minutes that will be added to the delay in minutes variable which is specified for each job, The minimum delay value is set, by default, to 6 minutes. If RANDOM_DELAY is, for example, set to 12, then between 6 and 12 minutes are added to the delay in minutes for each job in that particular anacrontab. RANDOM_DELAY can also be set to a value below 6, including 0. When set to 0, no random delay is added. This proves to be useful when, for example, more computers that share one network connection need to download the same data every day.

So if you want to schedule system maintenance cron jobs to run at off peak times you will have to tweak the START_HOURS_RANGE in /etc/anacrontab Or if you want more fine grained control you could just ditch anacron altogether and use /etc/cron.d/dailyjobs

# yum remove cronie-anacron

# yum install cronie-noanacron sysstat

Don’t forget to start crond!

# vim /etc/cron.d/dailyjobs

# service crond start

# chkconfig crond on

Advertisements

December 13, 2012 - Posted by | Cron, Tips & Tricks, Unix/Linux | , , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: