LinuxCommandLibrary

anacron

Run jobs periodically, even when system is off

SYNOPSIS

anacron [options] [job_identifier]
anacron -t anacrontab_file [-S spool_dir] [options] [job_identifier]

PARAMETERS

-s
    Run jobs sequentially.
Executes the jobs defined in anacrontab one after another, waiting for each to finish before starting the next. This is the most commonly used option when anacron is invoked.

-f
    Force execution.
Forces the execution of all jobs, regardless of their timestamps.

-n
    Run immediately.
Ignores the random delay specified in anacrontab and runs jobs immediately after the initial delay.

-u
    Update timestamps only.
Updates the timestamps of all jobs to the current date, but does not actually run them. Useful for resetting job schedules.

-V
    Verbose output.
Prints more information about what anacron is doing to standard output.

-d
    Debug mode.
Similar to verbose mode but also prints error messages to standard error. Implies -V.

-t anacrontab_file
    Specify anacrontab file.
Uses the specified file as the anacrontab instead of the default /etc/anacrontab.

-S spool_dir
    Specify spool directory.
Uses the specified directory to store timestamp files instead of the default /var/spool/anacron.

-q
    Suppress messages.
Suppresses all messages to standard output and standard error.

job_identifier
    Run a specific job.
If provided, anacron will only run the job matching this identifier from the anacrontab file.

DESCRIPTION

anacron is a utility that executes commands periodically, much like cron, but with a crucial difference: it's designed for systems that are not running 24/7. Unlike cron, which relies on strict time schedules, anacron ensures that jobs are run even if the machine was powered off during their scheduled execution time.

When anacron is run (typically once a day via cron itself, often from /etc/cron.daily/00anacron or /etc/cron.hourly/anacron), it checks its configuration file, anacrontab (defaulting to /etc/anacrontab). For each job listed, it verifies if it has been executed within its defined period. If a job has not been run for longer than its specified period (e.g., a weekly job hasn't run in over 7 days), anacron will execute it.

To prevent resource spikes, anacron can apply a random delay before executing a job. It also updates a timestamp file (in /var/spool/anacron by default) after each successful job execution to track when it was last run. This makes anacron ideal for laptops, home servers, or any system that isn't always powered on, guaranteeing that maintenance tasks, backups, and log rotations eventually run.

CAVEATS

anacron typically relies on cron to be started daily. If cron is not running or the daily/hourly anacron script is removed, anacron jobs won't execute.

Jobs are executed with the permissions of the user running anacron (usually root when invoked from system cron).

Jobs are not guaranteed to run at a precise time, only that they will run eventually after the machine has been on for a sufficient duration and the initial delay has passed.

The system clock must be reasonably accurate for anacron to function correctly regarding its period checks.

<I>ANACRONTAB FILE FORMAT</I>

The anacrontab file (e.g., /etc/anacrontab) defines the jobs anacron manages. Each line typically has the format:
perioddelayjob-identifiercommand

  • period: The frequency in days (e.g., 7 for weekly, 30 for monthly, 1 for daily). Special values like @daily, @weekly, @monthly can also be used, which are equivalent to 1, 7, 30 days respectively.
  • delay: The number of minutes anacron waits after starting before executing the job. This helps to spread out system load.
  • job-identifier: A unique string for the job, used to create its timestamp file.
  • command: The shell command to be executed.

<I>TIMESTAMP FILES</I>

anacron uses timestamp files, typically stored in /var/spool/anacron (or a specified spool directory), to record the last execution date of each job. These files are named after their job-identifier. anacron checks these timestamps against the current date and the job's defined period to decide if a job needs to be run.

<I>TYPICAL INVOCATION</I>

On most Linux distributions, anacron is not started manually by users. Instead, it's typically invoked by the system's cron daemon, usually daily or hourly. For example, a script like /etc/cron.daily/00anacron might contain:
#!/bin/sh
test -x /usr/sbin/anacron || exit 0
/usr/sbin/anacron -s
This ensures that anacron checks for missed jobs whenever the system performs its daily cron tasks.

HISTORY

anacron was developed to address a common problem with traditional cron scheduling: jobs would be missed on systems that were not continuously powered on, such as laptops. It first appeared in Debian and was subsequently adopted by other Linux distributions. Its design focuses on ensuring eventual execution of periodic tasks, making it a vital component for maintaining intermittently active machines. It gained widespread adoption as Linux became more common on desktop and portable devices.

SEE ALSO

cron(8), crontab(5), at(1), systemd.timer(5)

Copied to clipboard