What is anacron and how its different from cron?
Anacron is used to execute commands periodically, with a frequency specified in days. Unlike cron, it does not assume that the machine is running continuously. Hence, it can be used on machines that are not running 24 hours a day to control regular jobs as daily, weekly, and monthly jobs. Anacron tries to run the scheduled jobs as close as the system uptime permits.
anacron configuration file
/etc/anacrontab is the anacron configuration file. Below is a sample, unedited anacron file :
# cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. 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=3-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
1. period in days : specifies the frequency of execution of a job in days. This variable can be represented by an integer or a macro (@daily, @weekly, @monthly), where @daily denotes the same value as the integer 1, @weekly the same as 7, and @monthly specifies that the job is run once a month, independent on the length of the month.
2. delay in minutes: specifies the number of minutes anacron waits, if necessary, before executing a job. This variable is represented by an integer where 0 means no delay.
3. job-identifier: specifies a unique name of a job which is used in the log files.
4. command: specifies the command to execute. The command can either be a command such as ps -ef >> /tmp/processes or a command to execute a custom script.
The 3 lines at the end of the configuration files are system defined cron jobs.
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
Rest of the variables are explained with an example below.
Example of anacron configuration
Let us check an example of configuring anacron. We are creating anacron job to run a script named “daily_job.sh” daily with a 35 min delay after the system comes up.
# cat /etc/anacrontab RANDOM_DELAY=30 START_HOURS_RANGE=10-18 1 35 daily_job sh /var/tmp/daily_job.sh
If the system is running then job will run as per START_HOURS_RANGE which is defined in /etc/anacrontab file. The START_HOURS_RANGE variable defines the range of hours in which the sheduled jobb is allowed to run. In our case it is 10 A.M. to 6 P.M. (10-18)
# grep -i START_HOURS_RANGE /etc/anacrontab START_HOURS_RANGE=10-18
The RANDOM_DELAY variable denotes the maximum number of minutes that will be added to the delay in minutes variable which is specified for each job. A RANDOM_DELAY set to 30 would therefore add, randomly, between 0 and 30 minutes to the delay in minutes for each job in that particular anacrontab. When set to 0, no random delay is added.
# grep -i RANDOM_DELAY /etc/anacrontab RANDOM_DELAY=30