Scheduling tasks using anacron in Linux/Unix

|
Last Updated:
|
|

Well, this guide will focus on scheduling tasks using anacron in Linux/Unix systems.

So what is anacron? Anacron is a service that can be used to execute scheduled tasks or jobs or commands periodically in Linux/Unix systems with a frequency specified in days.

Schedule tasks using Anacron in Linux/Unix

In our previous guide, we discussed how to schedule system tasks or jobs in Linux/Unix using the cron command. Check the link below;

How to Schedule Cron Jobs/Tasks in Linux/Unix

So, how does anacron and cron services differ?

  • While cron is suitable for scheduling tasks on systems that are rarely shut down, anacron is suitable for scheduling asynchronous tasks on systems that are shut down regularly.
  • Cron jobs are executed at the exact time they are scheduled and if the system is shutdown when the cron job is supposed to be being executed, that cron is skipped until the next schedule.
  • Anacron jobs will be postponed until when the system is running if the system is shutdown by the time they were scheduled to be executed.
  • Anacron jobs are executed at most once a day while cron jobs can be executed more than once in a day.

Files used by Anacron

  • Anacron reads scheduled jobs from /etc/anacrontab configuration file.
  • It also uses the /var/spool/anacron directory for storing timestamp files on when a specific job was last executed.

Anacron Job Entry Format

Each anacron job entry is specified in the format;

period delay job-identifier shell-command

Where:

  • period specifies how frequently, in days the job should be executed. For a example, a value of 10 means the job should be run every 10 days.
  • delay specifies a time in minutes between the when the anacron starts and when the job is executed.
  • job identifier specifies a unique job ID. This is used in log messages to identify the jobs.
  • shell command specifies the command or the name of the script to be executed.

How does anacron work?

Anacron operates by checking whether a scheduled job has been executed in the last days specified in the period parameter. If the job has not been executed, anacron waits for the number of minutes specified in the delay field to elapse after which it runs the specified shell command or script.

Every anacron job that is being executed or that is awaiting execution is locked so that if there are any other copies of Anacron in the system, such tasks cannot be executed at the same time.

Once the execution of the specified task or job completes, Anacron timestamps this and exits when there is no more scheduled jobs to be executed.

Output from Anacron tasks are mailed to the root user or to the user specified by the MAILTO environment variable if it is defined.

By default, Anacron jobs cannot be executed if the system is not plugged in to power source. This ensures that the battery is not drained when executing the scheduled anacron jobs.

Note that anacron jobs can also be called via the cron jobs.

Schedule tasks using anacron

Before we can learn how to schedule an anacron job, let us have a look at a sample Anacron table, also known as, anacrontab file from a CentOS 7 system.

less /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

Based on the format above, for example, to schedule an anacron job to execute a;

  • backup shell script called db-backups.sh
  • with a unique identifier of my-backups
  • every 7 days
  • with a delay of 10 minutes between when anacron runs and when the script runs,

Simply add the line below on the /etc/anacrontab file.

7 10 my-backups /path/to/db-backups.sh

To use custom anacrontab for example, you can create is as follows;

echo "1 1 backup /home/me/myscripts/backup.sh" > mycustomanacrontab

Examples on Scheduling tasks using anacron in Linux/Unix

To wrap up with, let us see example usage of various anacron command line options.

Anacron Command Syntax

The anacron command line syntax is like;

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]

To explain the usage of some of these options, let us see a few examples;

Test the validity of anacron configuration

Once you have schedules your anacron job, you can run the command below to verify its validity.

anacron -T

If there is no error in your configuration, this command will give not output.
Assume you install an anacron task on your anacrontab called myanacrontest on your home directory with the line below missing the job identifier;

1 0  /home/me/myscripts/backup.sh

Run the validity test by running;

anacron -T -t myanacrontest

Output

anacron: Invalid syntax in myanacron on line 1 - skipping this line

Force execution of anacron job

You can force the execution of anacron job by using option -f. For example, to force the execution of anacron job installed on a custom directory and sent the informational messages to standard error while being mailed to the default user;

anacron -d -f -t myanacron

Run anacron jobs one after the other

To execute anacron jobs one after the first one completes, you can use the -n or -s options where:

  • -n means execute the jobs immediately
  • -s means serialize execution of jobs. Anacron will not start a new job before the previous one finished.
anacron -d -n -t myanacron
anacron -d -s -t myanacron

Both give the same output;

Anacron started on 2019-09-05
Normal exit (0 jobs run)

That is just it on our guide on scheduling tasks using anacron in Linux/Unix. Feel free to read more about anacron command line options on man anacron.

In our next guide, we will learn how to schedule tasks using the at command. See the link below;

Scheduling tasks using at command in Linux

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

1 thought on “Scheduling tasks using anacron in Linux/Unix”

Leave a Comment