OSETC TECH

Linux Anacron (cron.daily, cron weekly, cron.monthly)

This post will guide you how to use anacron tool to run commands or your own scripts periodically under Linux operating system. How do I setup anacron jobs in its configuration file /etc/anacrontab in Linux. How to run daily, weekly, and monthly scheduled jobs with anacron tool in Linux.

What is Anacron?


NAME

anacron – runs commands periodically

SYNOPSIS

anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job]
anacron [-S spooldir] -u [-t anacrontab] [job]
anacron [-V|-h]
anacron -T [-t anacrontab]

DESCRIPTION

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 reads a list of jobs from the /etc/anacrontab configuration file (see anacrontab(5)). This file contains the list of jobs that Anacron controls. Each job entry specifies a period in days, a delay in minutes, a unique job identifier, and a shell command.

/etc/anacrontab – configuration file for Anacron


The /etc/anacrontab configuration file describes the jobs controlled by anacron(8). It can contain three types of lines: job-description lines, environment assignments, or empty lines.

Job-description lines can have the following format:

period in days    delay in minutes    job-identifier      command

The period in days variable 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.

The delay in minutes variable 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.

The job-identifier variable specifies a unique name of a job which is used in the log files.

The command variable specifies the command to execute. The command can either be a command such as ls /proc >> /tmp/proc or a command to execute a custom script.

Issue the following command to check the configuration file /etc/anacrontab:

#cat /etc/anacrontab

Outputs:

[root@osetc_x8664 ~]# 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

Where,

The START_HOURS_RANGE variable will define an interval when the scheduled jobs can be run in your system.

The RANDOM_DELAY variable will denote the maximum number of minutes that will be added to the delay in minutes variable. and a random value between 0 and 45 will be added to the startup delay of each job.

Cron.daily will run at 3:05AM

Cron.weekly will run at 3:25AM

Cron.monthly will run at 3:45AM

Linux Anacron Example


This example shows how to set up an Anacron job similar in functionality to /etc/crontab which starts all regular jobs between 6:00 and 8:00 only. A RANDOM_DELAY which can be 30 minutes at the most is specified. Jobs will run serialized in a queue where each job is started only after the previous one is finished. You need to edit the /etc/anacrontab file with vi/vim editor, type:

#vim /etc/anacrontab

Adding the below lines into the configuration file:

# environment variables

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

RANDOM_DELAY=30

# Anacron jobs will start between 6am and 8am.

START_HOURS_RANGE=6-8

# delay will be 5 minutes + RANDOM_DELAY for cron.daily

1 5 cron.daily nice run-parts /etc/cron.daily

7 0 cron.weekly nice run-parts /etc/cron.weekly

@monthly 0 cron.monthly nice run-parts /etc/cron.monthly