anacron

system managementlinux
The anacron command is one of the most frequently used commands in Linux/Unix-like operating systems. anacron The anacron command runs commands periodically with a frequency specified in days, executing tasks that may have been missed while the system was powered off, unlike cron which requires the system to be running at the scheduled time.

Quick Reference

Command Name:

anacron

Category:

system management

Platform:

linux

Basic Usage:

anacron [options] [arguments]

Common Use Cases

  • 1

    Scheduled task execution

    Run tasks at specified intervals even when system is not always on

  • 2

    System maintenance

    Schedule regular maintenance tasks like log rotation and cleanup

  • 3

    Backup automation

    Automate backup tasks that need to run periodically

  • 4

    System monitoring

    Schedule health checks and monitoring tasks

Syntax

anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job...]

Options

Option Description
-f, --force Run jobs regardless of when they were last run
-n, --now Run jobs immediately, ignoring the delay specifications
-d, --debug Print debugging messages to stderr
-s, --serialize Serialize jobs, run only one job at a time
-q, --quiet Suppress messages to stdout
-t FILE, --tab FILE Use FILE as the anacrontab
-S SPOOLDIR, --spool SPOOLDIR Use SPOOLDIR as the spool directory
-T, --test Only test the configuration file, don't run any jobs
-h, --help Display help message and exit
-V, --version Display version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the anacron command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Run all jobs in /etc/anacrontab
sudo anacron
Force all jobs to run, ignoring timestamps
sudo anacron -f
Run a specific job from /etc/anacrontab
sudo anacron cron.daily
Run now, ignoring any delay specified in the config
sudo anacron -n
Run in debugging mode (verbose output)
sudo anacron -d

Advanced Examples:

Use an alternative anacrontab file
sudo anacron -t /path/to/custom/anacrontab
Use a different spool directory
sudo anacron -S /var/spool/custom-anacron
Run a job now, with all options
sudo anacron -f -n -d cron.weekly
Run quietly with no messages to stdout
sudo anacron -q
Run serially, only starting the next job when the previous one completes
sudo anacron -s

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Understanding Anacron:

Anacron is designed to run periodic commands on systems that aren't powered on 24/7. Key differences from cron:

  • Anacron works with job frequencies in days, not arbitrary times
  • If a scheduled job is missed because the computer was off, anacron will run it when the computer is next turned on
  • Anacron itself is typically run from cron or at system startup
  • Anacron doesn't run continuously in the background; it executes scheduled jobs and then exits

Anacrontab File Format:

The default anacrontab file is /etc/anacrontab. The format is:

# period  delay  job-identifier  command
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

Where:

  • period: Frequency in days (how often the job should run)
  • delay: Delay in minutes after system startup before the job runs
  • job-identifier: Unique name for the job (used in timestamps and logs)
  • command: Command to execute

Environment Variables in Anacrontab:

The anacrontab file can include environment variable settings:

# Environment variables
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=45
START_HOURS_RANGE=3-22

Where:

  • SHELL: Shell to use for executing commands
  • PATH: Search path for commands
  • MAILTO: User to mail output to (empty for no mail)
  • RANDOM_DELAY: Maximum random delay added to the base delay (in minutes)
  • START_HOURS_RANGE: Time range when jobs may start (e.g., 3-22 means between 3am and 10pm)

Timestamps and Job Management:

Anacron keeps track of when jobs were last run using timestamp files:

  • Timestamps are stored in /var/spool/anacron/ by default
  • Each job has a timestamp file named after the job-identifier
  • When anacron runs, it compares the current date with the stored timestamp to determine if a job should run
  • To force a job to run regardless of its timestamp, use the -f option

Common Use Cases:

  • Running system maintenance tasks on laptops or desktops that aren't always on
  • Scheduling backups that should happen every few days
  • Running update checks on a regular basis regardless of system uptime
  • Cleaning temporary files periodically on machines with irregular usage patterns

Setup and Integration:

Anacron is typically invoked:

  • At system startup via a systemd service or init script
  • Periodically through cron (usually hourly)
  • Manually when needed

Important Notes:

  • Anacron requires root privileges to access its configuration and timestamp files
  • Jobs defined in /etc/anacrontab run as root by default
  • If a system is always running, cron may be more appropriate for tasks that need precise timing
  • On most modern distributions, anacron automatically runs jobs in /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly
  • Output from anacron jobs is typically mailed to the root user unless configured otherwise
  • For personal use on desktop systems, consider creating a user-specific anacrontab file

Common Use Cases

Scheduled task execution

Run tasks at specified intervals even when system is not always on

System maintenance

Schedule regular maintenance tasks like log rotation and cleanup

Backup automation

Automate backup tasks that need to run periodically

System monitoring

Schedule health checks and monitoring tasks

Desktop task scheduling

Schedule tasks on desktop systems that may be powered off

Related Commands

These commands are frequently used alongside anacron or serve similar purposes:

Use Cases

1

Scheduled task execution

Run tasks at specified intervals even when system is not always on

2

System maintenance

Schedule regular maintenance tasks like log rotation and cleanup

3

Backup automation

Automate backup tasks that need to run periodically

4

System monitoring

Schedule health checks and monitoring tasks

5

Desktop task scheduling

Schedule tasks on desktop systems that may be powered off

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the anacron command works in different scenarios.

$ anacron
View All Commands