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.
# the maximal random delay added to the base delay of the jobs
# the jobs will be started during the following hours only
#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
Cron and Anacron has its own advantages and disadvantages. Depending on your requirement, use one of them.
* Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)
* Cron job can be scheduled by any normal user ( if not restricted by super 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.
* Ideal for servers.
* Use cron when a job has to be executed at a particular hour and minute.
* Minimum granularity is only in days.
* Anacron can be used only by super user ( but there are workarounds to make it usable by normal user ).
* 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 desktops and laptops.
* Use anacron when a job has to be executed irrespective of hour and minute.