Overview
anacron executes jobs defined in the `/etc/anacrontab` file on a daily, weekly, or monthly basis. If the system was off during the scheduled execution time, anacron runs the missed jobs upon system restart, preventing job omissions. This differentiates it from `cron`, which is ideal for systems running 24/7.
Key Features
- Prevents job omissions when the system is not running
- Executes jobs at scheduled intervals (daily/weekly/monthly)
- Tracks the last execution time based on log files
- Primarily useful for desktop and laptop environments
Main Options
These are the main options used with the anacron command.
Execution Control
Configuration/Information
Generated command:
Try combining the commands.
Description:
`anacron` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Here are some common examples of using anacron.
Manual Anacron Execution
sudo anacron -s
Manually executes all jobs defined in the anacrontab. While it runs automatically on system boot, you can trigger it manually when needed.
View Anacrontab File
cat /etc/anacrontab
Checks the configuration file where anacron jobs are defined.
Run in Debug Mode
sudo anacron -d
Executes anacron in debug mode to get detailed information about which jobs are being run.
Update Timestamp Without Running Jobs
sudo anacron -u
Updates the last execution time to the current time without actually running the jobs. This is useful if you want to skip certain jobs.
Installation
anacron is usually installed by default as part of the `cronie` or `vixie-cron` package on most Linux distributions. If it's not installed, you can install it using the following commands.
Debian/Ubuntu Based
sudo apt update && sudo apt install cronie
Installs the `cronie` package using the apt package manager.
RHEL/CentOS/Fedora Based
sudo dnf install cronie
# or
sudo yum install cronie
Installs the `cronie` package using the dnf or yum package manager.
Tips & Notes
Useful tips and points to note when using anacron.
Key Configuration Files and Directories
These are the main files and directories that control anacron's behavior.
- /etc/anacrontab: The main configuration file where the period, delay, job identifier, and command to be executed for anacron jobs are defined.
- /var/spool/anacron/: The directory where timestamp files are stored, recording the last execution time for each job. anacron uses these files to determine if jobs have been missed.
Difference from cron
`cron` executes jobs at scheduled times when the system is always on, whereas `anacron` runs missed jobs after a delay when the system boots up. Therefore, `anacron` is more suitable for environments where the system is not always running, such as laptops or desktops. `anacron` can be used in conjunction with `cron`, where `cron` might be configured to run `anacron` periodically to check for missed jobs.
anacrontab File Structure
Each line in the `anacrontab` file is structured as `period delay job-identifier command`.
- period: The execution interval for the job (e.g., 1 for daily, 7 for weekly, @monthly for monthly).
- delay: The time (in minutes) to wait after system boot before executing the job. This helps reduce system load.
- job-identifier: A unique string that identifies the job. A timestamp file with this name is created in `/var/spool/anacron/`.
- command: The shell command to be executed.