anacron runs commands on a regular basis, and the operating frequency is defined in units of days. It is suitable for computers that do not run 24/7, such as laptops and desktops. Suppose you have a scheduled task (such as a backup script) to be run in the early morning of every day using crontab. When you fall asleep, your desktop or notebook is off. Your backup script will not run. However, if you use anacron, you can rest assured that the next time you turn on the desktop or notebook, the backup script will run.**
The appearance of anacron is not to replace crontab, but to complement crontab. Their relationship is as follows:
shell>cat/etc/anacrontab
# /etc/anacrontab: configuration file for anacron# See anacron(8) and anacrontab(5) for details.SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# Default 45 minutes delay for each specified job anacron random increase 0-45 minutes.RANDOM_DELAY=45# Specify the scope of work time, represented here 3:00 ~ 22:00START_HOURS_RANGE=3-22
# period in days delay in minutes job-identifier command# Boot every day to check whether the files in the directory /etc/cron.daily be executed in 5 minutes, if not executed today, then to the next15cron.dailynicerun-parts/etc/cron.daily
# Every 7 days within 25 minutes if the file check /etc/cron.weekly directory is executed after boot, if not executed within a week, it will be executed next725cron.weeklynicerun-parts/etc/cron.weekly
# Whether the files in the directory /etc/cron.monthly 45 minutes checking is performed after every start for a month
@monthly45cron.monthlynicerun-parts/etc/cron.monthly
/etc/cron.hourly/ -Through journalctl -u crond.service, you can know that the files put inside are actually called by crond.server, which means that the command will be executed after the first minute of every hour. As follows:
shell>cat/etc/cron.d/0hourly
# Run the hourly jobsSHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01****rootrun-parts/etc/cron.hourly
To make certain files run within these automatically defined times, all you need to do is to copy the script file to the relevant directory and verify that it has execution permission (chmod +x). Therefore, you only need to let the system automatically run the script at one of these scheduled times, which simplifies the automation task.
Let us use cron.daily to illustrate the run process of /etc/anacrontab:
anacron reads the /var/spool/anacron/cron.daily file, and the content of the file shows the time of the last run.
Compared with the current time, if the difference between the two times exceeds 1 day, the cron.daily job will run.
This work can only run from 03:00-22:00.
Verify whether a file runs after 5 minutes after booting. When the first one runs, it will be randomly delayed for 0~45 minutes to run the next one.
Use the nice parameter to specify the default priority, and use the run-parts parameter to run all executable files in the /etc/cron.daily/ directory.