anacron Command in Linux



anacron command is a Linux utility that is used to execute commands periodically, similar to the one done by the cron. However, anacron has one main advantage over the cron that is it can be run on the machine that is not running continuously. The anacron runs the scheduled tasks at a specified time frame, without assuming whether a machine is always up and running.

The working of anacron is similar to cron, however, unlike cron that runs tasks based on a fixed schedule. If a task is not performed on the given time frame due to the machine being off, cron will not run the task until the next scheduled time. On the other hand, anacron checks the timestamp of each job and if its timestamp is older than the specified number of days, it runs the job after a delay.

Table of Contents

Important Files of anacron Command in Linux

There are two important files of anacron utility; /etc/anacrontab and /var/spool/anacron directory. Let’s discuss them first.

/etc/anacrontab

This file of anacron uses the job specifications. You can add your jobs inside this file and make them ready to be executed. It uses the sh shell apart from bash, thus you won’t be able to execute bash script directly. The table at the end of the anacrontab file represents different scheduling intervals, including days and time delays −

anacron Command Linux 1

/var/spool/anacron

At the /var/spool/anacron location, you will find timestamp files that anacron stores. These files represent timestamps for different job categories, days, weekly and monthly.

anacron Command Linux 2

anacron Command Linux 3

Syntax for anacron Command in Linux

The basic syntax for anacron command on Linux is provided below −

anacron [options] [job]

Here, the options you can use with anacron command are provided below −

Option Description
-fForces the execution of jobs and ignores the timestamp.
-uUpdates the job's timestamp to the current date, however, it doesn’t run anything.
-sSerializes the job’s execution, means it executes one job at a time.
-nRuns the jobs immediately without keeping in mind any delay associated with the job.
-dRuns the job in the foreground and displays the message on the terminal screen.
-qSuppress the error message and can only be used with the -d option.
-VPrints the version of the anacron utility on the terminal.
-hOpens up the help section for usage on the terminal.

Working of anacron Command in Linux

As previously mentioned that anacron jobs are listed in the file anacrontab location at /etc location on Linux. To add your desired jobs, you can use the following syntax −

period delay job-id command

Here in the above format,

  • period is the job’s execution frequency specified in days (@days), week (@week) or month (@monthly). Besides using the @days, @weekly and @monthly, you can use numbers 1, 7 and 30, respectively.
  • delay is the waiting time before executing the job.
  • job-id is a specific name provided to your job according to yourself.
  • command is the command or shell script that needs to be executed.

Example of anacron Command in Linux

Here, we will discuss a single example and elaborate a few important options that can be used with the anacron command on Linux. As an example, we are scheduling a job that appends a message inside in the quotes into the file named myfile.txt.

@daily 1 example.daily /bin/bash -c "echo 'anacron job is successfully executed' >> ~/myfile.txt"

You can create your own job and add the job inside the /etc/anacrontab file, which you can open in Linux from the following command −

sudo nano /etc/anacrontab
anacron Command Linux 4

Once done, save your file.

Now, you can use anacron command to update the timestamp of jobs and observer the changes in the terminal through the following command −

sudo anacron -d -u
anacron Command Linux 5

To perform force execution of jobs on Linux, you can use the following command −

sudo anacron -d -f

This will start the job execution on the terminal −

anacron Command Linux 6

For executing the job sequentially in Linux, you can use the below-given command −

sudo anacron -d -s
anacron Command Linux 7

For checking the version of the anacron command, use the following command −

sudo anacron -V
anacron Command Linux 8

This is how you can use the anacron command on Linux to execute jobs.

Conclusion

anacron command is a powerful Linux command that is an ideal replacement of the cron command. This article has covered the basics of anacron command, which include understanding of two important files of anacron, syntax, working and an example of anacron command.

In the end, all important options of anacron are executed in the terminal to help users understand the working of the command on Linux systems.

Advertisements