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 tutorial, 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 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.

Comparison

Feature Cron Anacron
System Uptime Requires system to be always running Works on intermittently running systems
Scheduling Time-based (specific minutes, hours, days) Period-based (daily, weekly intervals)
Missed Jobs Jobs are skipped if system is down Executes missed jobs when system starts
Precision Minute-level precision Day-level precision
Best For Servers, always-on systems Laptops, desktops, workstations

Installing and Configuring Anacron

Step 1: Install Anacron

First, ensure that Anacron is installed on your Linux system. Open a terminal and run:

sudo apt-get install anacron

For Red Hat-based systems, use:

sudo yum install anacron

Step 2: Create a Job Script

Create a simple bash script that we want to schedule. Create a file called myjob.sh:

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

Make the script executable:

chmod +x myjob.sh

Step 3: Configure Anacron

Anacron uses the /etc/anacrontab configuration file to define jobs and their scheduling intervals. Edit this file:

sudo nano /etc/anacrontab

The anacrontab file uses the following format:

period delay job-identifier command

Where:

  • Period Time in days between successive runs of the job

  • Delay Time in minutes to wait after system boot before running the job

  • Job Identifier Unique identifier for the job (no spaces allowed)

  • Command The command or script to execute

Add the following line to schedule your job:

1 5 myjob /path/to/myjob.sh

This configures a job that runs daily (period=1), waits 5 minutes after boot (delay=5), has identifier "myjob", and executes the specified script.

Step 4: Test the Configuration

To test your configuration immediately, run:

sudo anacron -f

This forces Anacron to run all scheduled jobs immediately. Check the log file to verify execution:

cat /var/log/myjob.log

Example: Cleanup Script

Here's a practical example of scheduling a cleanup job that runs every 30 days:

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

This entry schedules a cleanup script to run every 30 days with a 5-minute delay after system boot. The job identifier is "cleanup".

Key Points

  • Anacron maintains timestamps in /var/spool/anacron/ to track job execution

  • Jobs are executed only if the specified period has elapsed since the last run

  • Anacron runs as a system service and is typically started during boot

  • Unlike cron, anacron doesn't require the system to be running at specific times

Conclusion

Anacron is an essential tool for scheduling jobs on systems that are not always running, such as laptops and desktops. Unlike cron, anacron ensures that jobs are executed even if the system was shut down during the scheduled time, making it ideal for intermittently used systems. By understanding the differences between cron and anacron, administrators can choose the right tool for their specific scheduling needs.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements