跳至内容

anacron - 自动化命令

先决条件

  • 一台运行 Rocky Linux 的计算机。
  • 知道如何使用你喜欢的编辑器在命令行环境中修改配置文件(例如 vim)。
  • 理解基本的 RPM 包管理。

假设

  • 您具备 bash、python 或其他脚本或编程工具的基础知识,并希望自动运行脚本。
  • 您已作为 root 用户登录,或者使用 su - root 切换到 root。

简介

anacron 定期运行命令,其运行频率以天为单位。它适用于不 24x7 运行的计算机,例如笔记本电脑和台式机。假设您每天凌晨使用 crontab 安排一个任务(例如备份脚本)运行。当您睡觉时,您的台式机或笔记本电脑处于关闭状态。您的备份脚本将不会运行。但是,如果您使用 anacron,您可以放心,下次打开台式机或笔记本电脑时,备份脚本将运行。**

anacron 的出现并非是为了取代 crontab,而是为了补充 crontab。它们的关系如下:

 Relations

anacron 配置文件

shell > rpm -ql cronie-anacron
/etc/anacrontab
/etc/cron.hourly/0anacron
/usr/lib/.build-id
/usr/lib/.build-id/0e
/usr/lib/.build-id/0e/6b094fa55505597cb69dc5a6b7f5f30b04d40f
/usr/sbin/anacron
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man8/anacron.8.gz
/var/spool/anacron
/var/spool/anacron/cron.daily
/var/spool/anacron/cron.monthly
/var/spool/anacron/cron.weekly

首先检查默认配置文件

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:00
START_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 next
1 5 cron.daily nice run-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 next
7 25 cron.weekly nice run-parts /etc/cron.weekly
# Whether the files in the directory /etc/cron.monthly 45 minutes checking is performed after every start for a month
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

/etc/cron.hourly/ - 通过 journalctl -u crond.service,您可以知道放入其中的文件实际上是由 crond 调用,这意味着命令将在每小时的第一分钟之后运行。如下所示:

shell > cat /etc/cron.d/0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 *  *  *  * root run-parts /etc/cron.hourly
shell > journalctl -u crond.service
- Logs begin at Wed 2021-10-20 19:27:39 CST, end at Wed 2021-10-20 23:32:42 CST. -
October 20 19:27:42 li systemd[1]: Started Command Scheduler.
October 20 19:27:42 li crond[733]: (CRON) STARTUP (1.5.2)
October 20 19:27:42 li crond[733]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 76% if used.)
October 20 19:27:42 li crond[733]: (CRON) INFO (running with inotify support)
October 20 20:01:01 li CROND[1897]: (root) CMD (run-parts /etc/cron.hourly)
October 20 21:01:01 li CROND[1922]: (root) CMD (run-parts /etc/cron.hourly)
October 20 22:01:01 li CROND[1947]: (root) CMD (run-parts /etc/cron.hourly)
October 20 23:01:01 li CROND[2037]: (root) CMD (run-parts /etc/cron.hourly)

有关更多配置文件信息,请浏览手册页

用户使用

要使某些文件在这些自动定义的时间内运行,您只需将脚本文件复制到相关目录并验证它具有**执行权限 (chmod +x)**。因此,您只需让系统在这些预定时间之一自动运行脚本,这简化了自动化任务。

让我们以 cron.daily 为例说明 /etc/anacrontab 的运行过程

  1. anacron 读取 /var/spool/anacron/cron.daily 文件,文件内容显示上次运行的时间。
  2. 与当前时间相比,如果两者的差值超过 1 天,则 cron.daily 作业将运行。
  3. 此作业只能在 03:00-22:00 之间运行。
  4. 验证文件是否在启动后 5 分钟内运行。当第一个运行时,下一个将随机延迟 0-45 分钟运行。
  5. 使用 nice 参数指定默认优先级,并使用 run-parts 参数运行 /etc/cron.daily/ 目录中的所有可执行文件。

使用 anacron 命令,常用选项有:

选项 描述
-f 运行所有作业,忽略时间戳
-u 将时间戳更新到当前时间,而不执行任何操作
-T 测试配置文件 /etc/anacrontab 的有效性

有关更多帮助信息,请浏览手册页

作者:李天赐

贡献者:Steven Spencer, Ganna Zhyrnova