What is Anacron and usage of Anacron in Linux
Anacron is a service that runs after every system reboot, checking for any cron and at scheduled jobs that were to run while the system was down and hence, have not yet run. It scans the /etc/cron.hourly/0anacron file for three factors to determine whether to run these missed jobs. The three factors are the presence of the /var/spool/anacron/cron.daily file, the elapsed time of 24 hours since anacron last ran, and the presence of the AC power to the system. If all of the three factors are affirmative, anacron goes ahead and automatically executes the scripts located in the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories, based on the settings and conditions defined in anacron’s main configuration file /etc/anacrontab. The default contents of the /etc/anacrontab file are displayed below:
nextstep4it@localhost:~$ cat /etc/anacrontab SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root RANDOM_DELAY=45 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
This file has five environment variables defined: the SHELL and PATH variables set the shell and path to be used for executing the scripts (defined at the bottom of this file); MAILTO defines the username or an email which is sent any output and error messages; RANDOM_DELAY expresses the maximum random delay in minutes (added to the base delay of the jobs as defined in the second column of the last three lines); and START_HOURS_RANGE states the range of hours when the jobs could begin.
The last three lines, in the above sample output, define the schedule and the scripts to be executed. The first column represents the period in days (or @daily, @weekly, @monthly, or @yearly) which anacron uses to check whether the specified job has been executed in this many days or period, the second specifies the delay in minutes for anacron to wait before executing the job, the third identifies a job identifier, and the fourth column specifies the command to be used to execute the contents of the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly files. Here the run-parts command is used to execute all files under the three directory locations at the default niceness.
For each job, anacron checks whether the job was run previously in the specified days or period (column 1) and executes it after waiting for the number of minutes (column 2) if it was not. Anacron may be run manually at the command prompt. For example, to run all the jobs that are scheduled in the /etc/anacrontab file but were missed, you can issue the following command:
Anacron stores its execution date in the files located in the /var/spool/anacron directory for each defined schedule.