• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

CentOS / RHEL : anacron basics (What is anacron and how to configure it)

by admin

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

Here :
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

Filed Under: CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 7 : How to modify Network Interface names
  2. uuidgen: command not found
  3. How to find the inode size of an ext2/ext3/ext4 filesystem?
  4. locale: command not found
  5. How to remove bonding Network configuration in CentOS/RHEL
  6. How to disable timeout in ssh during login prompt (login session inactivity) in Linux
  7. How to Remove/Delete All Packages from Channel(s) in SpaceWalk
  8. hlint: command not found
  9. Understanding Samba utilities – nmblookup, smbstatus, smbtar, testparm, wbinfo, smbget
  10. flash Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright