Cron Vs Anacron_ How to Schedule Jobs Using Anacron on Linux


In the world of Linux, scheduling tasks is an important task that administrators need to perform on a regular basis. This is done to automate the process of performing routine tasks and to ensure that they are executed at the right time. There are two popular tools that are used for scheduling tasks in Linux: cron and anacron. Both of these tools have their own unique features and benefits.

In this blog post, we will discuss the differences between cron and anacron, and then we will demonstrate how to use anacron to schedule tasks on a Linux system.

Cron vs. Anacron: What’s the difference?

Before we learn how to schedule jobs using Anacron, let’s look at the difference between Cron and Anacron.

Cron

Cron is a time-based job scheduler that runs on Unix-like operating systems. It is used to schedule jobs to run at specific times or intervals. Cron is ideal for scheduling jobs that need to run at regular intervals, such as hourly, daily, weekly, or monthly. Cron is a powerful and flexible tool, but it has one major limitation. It assumes that the system is always running, and if the system is shut down or restarted, any scheduled jobs will not be executed.

Anacron

Anacron, on the other hand, is a cron-like tool that is designed to work with systems that are not always running. Anacron is ideal for systems that are shut down or restarted on a regular basis, such as laptops or desktops. Anacron can also be used on servers, but it is not as efficient as cron for scheduling jobs that need to run at specific times.

Anacron works by keeping track of the last time a job was run. It then calculates the next time the job should be run based on the interval specified in the configuration file. If the system is shut down or restarted before the job can be run, Anacron will run the job the next time the system is started.

In this tutorial, we will cover how to install and configure Anacron on Ubuntu and demonstrate how to schedule a job using Anacron.

Using Anacron to Schedule Jobs

Now that we have discussed the differences between cron and anacron, let's take a look at how to use anacron to schedule jobs on a Linux system. We will assume that you have a basic understanding of the Linux command line.

Step 1: Install Anacron

The first step is to make sure that Anacron is installed on your Linux system. To do this, open a terminal window and type the following command −

sudo apt-get install anacron

This command will install Anacron on a Debian-based system. If you are using a different Linux distribution, the command may be slightly different.

Step 2: Create a Job

The next step is to create a job that we want to schedule using Anacron. In this example, we will create a simple bash script that will print the current date and time to a file. Open a text editor and create a new file called "myjob.sh". Add the following lines to the file −

#!/bin/bash echo "The current date and time is $(date)" >> /var/log/myjob.log

Save the file and make it executable by typing the following command −

chmod +x myjob.sh

Step 3: Configure Anacron

The next step is to configure Anacron to run our job. Anacron uses a configuration file called "anacrontab" to define jobs and their scheduling intervals. Open a terminal window and type the following command to edit the anacrontab file −

sudo nano /etc/anacrontab

This will open the anacrontab file in the Nano text editor. The anacrontab file has the following format −

period delay job-identifier command

Let's take a closer look at each of these parameters −

  • Period − The period parameter specifies the time in days between two successive runs of the job. For example, if you want to run a job every day, you would set the period to 1. If you want to run the job every week, you would set the period to 7.

  • Delay − The delay parameter specifies the time in minutes that Anacron will wait before running the job after the system has been booted. This parameter is useful if you want to stagger the execution of jobs to avoid overloading the system during startup. The default delay is 5 minutes.

  • Job Identifier − The job-identifier parameter is a unique identifier for the job. This parameter is used by Anacron to keep track of which jobs have already been executed. The job identifier should be unique for each job and should not contain any spaces.

  • Command − The command parameter is the command that will be executed when the job runs. This can be any valid shell command, script, or executable.

Add the following line to the bottom of the anacrontab file to define our job −

1 5 myjob /path/to/myjob.sh

This line defines a job with a period of 1 day and a delay of 5 minutes. The job identifier is "myjob", and the command is "/path/to/myjob.sh". This means that Anacron will run the job once a day, and if the system is shut down or restarted before the job can be run, Anacron will run it the next time the system is started, as long as at least 5 minutes have passed since the last time the job was run.

Save the anacrontab file and exit the text editor.

Step 4: Test the Job

The final step is to test the job to make sure that it is working correctly. To do this, open a terminal window and type the following command −

sudo anacron -f

This command will force Anacron to run all scheduled jobs immediately. Anacron will also run any jobs that were missed due to the system being shut down or restarted.

Check the /var/log/myjob.log file to make sure that the job ran correctly. You should see a line in the log file that contains the current date and time.

Scheduling a Job with Anacron

To schedule a job with Anacron, you need to create a new entry in the /etc/anacrontab configuration file. For example, let's say you want to schedule a job to delete old log files every 30 days. You would add the following line to the configuration file −

30 5 cleanup / usr/local/bin/cleanup-script.sh

This line specifies that the cleanup-script.sh script will be executed every 30 days with a delay of 5 minutes after the system has been booted. The job-identifier for this job is "cleanup".

After adding the entry, save the configuration file and exit the text editor.

Anacron will automatically run the job the next time the system is booted and every 30 days thereafter. If the system was not turned on during the scheduled time, Anacron will run the job the next time the system is turned on.

Conclusion

In this blog post, we have discussed the differences between cron and anacron, and we have demonstrated how to use Anacron to schedule jobs on a Linux system. Anacron is a useful tool for scheduling jobs on systems that are not always running, such as laptops or desktops. Anacron ensures that jobs are executed even if the system is shut down or restarted, as long as the delay specified in the configuration file has passed. With Anacron, you can automate routine tasks and ensure that they are executed at the right time, without having to worry about whether the system is running or not.

Updated on: 23-Jun-2023

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements