Open In App

anacron command in linux with examples

Last Updated : 27 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the job then decides whether to run it or not and if its timestamp is >=n(n is defined number of days) then runs it after a specified time delay.

It mainly constitutes of two important Files:

  1. /etc/anacrontab : It contains specifications of job.

    • See line SHELL=/bin/sh. It uses sh not bash you cannot run bash scripts.
    • Table at the last represents different scheduling. The first column is the number of days(n) i.e. period and second is a time delay in minutes.
  2. /var/spool/anacron : This directory is used by Anacron for storing timestamp files. It represents timestamp for different category of jobs i.e. daily, weekly, monthly, etc.

Syntax:

anacron [-s]  [-f]  [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job]
anacron [-S spooldir] -u [-t anacrontab] [job] ...
 anacron [-V|-h]
 anacron -T [-t anacrontab]

Options:

  • f : Used to force execution of the jobs, ignoring the timestamps.
  • u : Only update the timestamps of the jobs, to the current date, but don’t run anything.
  • s : Serialize execution of jobs. Anacron will not start a new job before the previous one finished.
  • n : Run jobs now.Ignore any delay.
  • d : Don’t fork to the background. In this mode, Anacron will output informational messages to standard error, as well as to syslog. The output of jobs is mailed as usual.
  • q : Suppress messages to standard error. Only applicable with -d.
  • V (Use specified anacrontab) : Print version information and exit.
  • h (Use specified anacrontab) : Print short usage message, and exit.

Note:

  • You can add any script to etc/cron.daily or etc/cron.weekly or cron.monthly directory.but remember script should be sh not bash.

  • Don’t forget to give executable permissions to your files sudo chmod +x filename.
  • Scripts in directories automatically get executed depending upon which directory they are in.

Examples:

  • Changing the timestamp of jobs. Observe the change in timestamp values.

  • Performing force execution. Notice the time delay.

  • Serialized Execution.

  • Print version information.

  • Print sort Usage message.

Note: Here we have used -d option with all commands to display on the screen what is actually happening.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads