How To Schedule Tasks using Anacron in Ubuntu

How to program tasks in Linux systems through the console using Anacron and cron.

What is Cron?

Cron is a Daemon that we can use to execute scheduled tasks such as directory backup, system backups, and installation of updates and is ideal for computers that work periodically as servers.

When using cron, the tasks are created in crontab files which are scripts that define the job to be executed, and the default path is /etc/crontab where, when accessing with an editor, we will see the respective tasks with their execution frequency:

But when using crontab we will be able to create our file of scheduled tasks as users, for this, we execute the following line:

crontab -e

When running this command we will be able to select up to five editor options to open the new file to be created (in this case we have selected option two nano), and we will see the following:

There it will merely be necessary to put the data of the task to be executed.

How to Install Anacron

For this tutorial, we will focus on Anacron which works differently than Cron since it allows the execution of tasks on computers that are used periodically and should not be active 24 X 7 like servers.

To install and get the most out of Anacron we will execute the following command:

sudo apt install anacron
Note: In versions, prior to Ubuntu 16.04 we will run the following command.
sudo apt-get install anacron

How To Use Anacron

The configuration parameters are hosted in the path /etc/anacrontab and the syntax to be used includes the following fields:

period delay job-identifier command
Note: All comments within Anacron should start with the # symbol.

When accessing the indicated route, we will see the following:

The values to use are:

  • Period: Indicates the frequency with which the task in question will be performed using parameters such as @daily (daily), @weekly (weekly) or @monthly (monthly), but we can also use settings 1 – daily, 7 – weekly, 30 – regularly and N – number of days in which the task will be performed.
  • Delay: Refers to the number of minutes before the task is executed.
  • Job Identifier: Indicates the name we will give to the task.
  • Command: Refers to the command or script to be executed.

To know the structure, we can execute the following line:

ls -l /var/spool/anacron/

Anacron verifies if a task has been achieved within the stipulated initial period, if not, proceed to run what we have indicated in the command field after having waited for the number of minutes shown in the delay field.

Once the task has been executed, it will be registered in the path/var/spool/anacron with its respective date and time.

For example, we can add the following line in the anacrontab file to schedule a daily backup task with an 8-minute delay:

@daily 8 augusto.daily /bin/bash /home/augusto/bin/backup.sh

With this case, if when the backup task is to be carried out, the machine is off, the next time it is started it will wait 8 minutes and perform the indicated task without waiting for the next day, etc.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *