Schedule tasks with Anacron in Ubuntu

Published: June 27, 2022

Schedule tasks with Anacron in Ubuntu

In this article, we would cover how to Schedule tasks with Anacron in Ubuntu. We use Anacron for tasks which need to be run at regular intervals. It's a bit different from cron daemon. Anacron is well-suited for a machine which do not run all the time or 24 hours in a day. If the machine runs 24 hours in a day then, we would prefer cron. Follow the article - Schedule tasks with cron in Ubuntu to know more about cron.

We need to first make sure the package anacron is installed on our machine. To check if its there or not -

anacron -V

If it returns with package version number and other related stuff. Then, it is there. Otherwise, we need to install the package.

Note: Following operations would require you to have superuser privileges. In case you don't have one then, we advise you to contact your System Administrator for assistance.

Install Anacron in Ubuntu

The package is already available through standard Ubuntu repository. Therefore, we need to update the repository first. This ensures we get to have the latest version of package installed on our machine. Hence, open a terminal and issue the following -

sudo apt update

Next, to install Anacron -

sudo apt install anacron

It will install all related dependencies with it.

Schedule tasks with Anacron in Ubuntu

The configuration file for Anacron -

/etc/anacrontab

It would contain a list of all tasks which the Anacron is expected to execute at a specific time and frequency. It starts with environment variable assignments. Then, there are four task related fields - period(days), delay(minutes), task-identifier and the command.

period delay task-identifier command

The first field, period - specifies the frequency of command execution.

delay - the most crucial feature here. delay holds task execution for a specific number of minutes after Anacron starts. Just imagine, if we asked the Anacron to execute hundreds of tasks at the same instant. Then, it may push our machine over the brink. Therefore, by delaying the execution - we can structure things in such a way that multiple tasks don't interfere with each other. For instance, we may execute task X with 2 minutes delay. Task Y to execute with 5 minutes delay. That way, task X and Y won't consume system resources at the same time.

task-identifier - specifies a name for log files.

command - the instructions we provide.

Apart from that, anacron monitors every task and records the date when the task was last run in a timestamp file. So, if a task doesn't executes within a specified time period. Then, the next time Anacron starts up it would execute the task.

Example

Let's understand how we can schedule tasks with Anacron with the help of an example now.

Create a script file - test_file and add the following code in it -

#!/bin/sh
touch /dev/shm/anacron_file

Save it to your desired location. And, make it executable -

chmod +x test_file

Thereafter, append /etc/anacrontab with the following -

1   1   techpiezo.daily   /bin/sh /home/techpiezo/scripts/test_file

Save and exit. What happens now?

Anacron would execute the script daily with delay of one minute. It creates a timestamp file - techpiezo.daily in the directory /var/spool/anacron

Script file - test_file would execute everyday to create an empty file in /dev/shm directory. It would, thereafter, update the timestamp file (i.e. techpiezo.daily) with the last run date. For instance, if its June 26, 2022 then you would see the following entry in /var/spool/anacron/techpiezo.daily -

20220627

In conclusion, we have discussed how to schedule tasks in Ubuntu through relevant example. We will discuss about various anacron options and difference between cron and anacron in coming articles.