Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using Anacron - Comprehensive Tutorial

Introduction

Anacron is a Linux utility used for running commands periodically with a frequency specified in days. Unlike cron, it ensures that the jobs run even if the computer is off during the time they were scheduled to run. This makes Anacron ideal for laptops or desktops that are not always powered on.

Installing Anacron

Before you can use Anacron, you need to ensure it is installed on your system. Use the following command to install Anacron:

sudo apt-get install anacron

For Red Hat-based distributions, use:

sudo yum install anacron

Basic Configuration

Anacron's configuration file is located at /etc/anacrontab. This file contains the schedule for the jobs. Each line in the file represents a job and has the following format:

period delay job-identifier command

Where:

  • period: The frequency in days the job should run (1 = daily, 7 = weekly, 30 = monthly).
  • delay: The delay in minutes after the system starts that the job should run.
  • job-identifier: A unique identifier for the job.
  • command: The command to be executed.

Example Configuration

Let's look at an example configuration:

1 5 cron.daily run-parts /etc/cron.daily
7 10 cron.weekly run-parts /etc/cron.weekly
30 15 cron.monthly run-parts /etc/cron.monthly

In this example:

  • The first job runs daily with a 5-minute delay after system startup and executes all scripts in /etc/cron.daily.
  • The second job runs weekly with a 10-minute delay and executes all scripts in /etc/cron.weekly.
  • The third job runs monthly with a 15-minute delay and executes all scripts in /etc/cron.monthly.

Adding Custom Jobs

To add a custom job, you can add a new line to the /etc/anacrontab file. For example, to run a backup script daily with a 20-minute delay, add the following line:

1 20 backup /usr/local/bin/backup-script.sh

Running and Managing Anacron Jobs

To manually run Anacron jobs, use the following command:

sudo anacron -f

The -f option forces Anacron to run all jobs immediately.

To check the status of Anacron jobs, you can look at the log file located at /var/log/syslog or /var/log/anacron depending on your distribution:

tail -f /var/log/syslog

Conclusion

With Anacron, you can ensure that your periodic jobs run even if your machine is not turned on at the scheduled time. This makes it a powerful tool for systems that are not always running.

By following this comprehensive tutorial, you should now have a good understanding of how to install, configure, and manage Anacron on your system.