Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions docs/guides/automation/anacron.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
title: anacron - Automating commands
author: tianci li
contributors: Steven Spencer
update : 2021-10-20
---

# anacron- Run commands regularly

## Prerequisites

* A machine running Rocky Linux.
* Know how to use your favorite editor to modify the configuration file (such as *vim* ) in the command line environment .
* Understand basic RPM package management

## Assumption

* You have understood the basic knowledge of bash, python or other scripting/programming tools, and want to run the script automatically.
* You are logged in as the root user, or switch to root with `su-root`

## Anacron Introduction
**anacron is used to run 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/laptop is shut down. Your backup script will not be executed. However, if you use anacron, you can rest assured that the next time you turn on the desktop/laptop, the backup script will be executed. **

The appearance of anacron is not to replace crontab, but to complement crontab. Their relationship is as follows:

![ Relations ](../images/anacron_01.png)

## anacron configuration file

```bash
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
```

First check the default configuration file:
```bash
shell > cat /etc/anacontab
# /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/** -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:

```bash
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)

```

For more configuration file information, please [Browse the manual page](https://man7.org/linux/man-pages/man5/anacrontab.5.html)
# # User use
In order 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 make sure that it has ** x execution permission (chmod +x) ** . Therefore, you only need to let the system automatically run the script at one of these scheduled times, which makes the automation task very easy.
Let's use cron.daily to illustrate the execution process of /etc/anacrontab:

1. Anacron reads the ** /var/spool/anacron/cron.daily ** file, and the content of the file shows the time of the last execution.
2. Compared with the current time, if the difference between the two times exceeds 1 day, the cron.daily job will be executed.
3. This work can only be performed from 03:00-22:00.
4. Check whether a file is executed after 5 minutes after booting. When the first one is executed, it will be randomly delayed for 0~45 minutes to execute the next one.
5. Use the nice parameter to specify the default priority, and use the run-parts parameter to execute all executable files in the /etc/cron.daily/ directory.

# # Related commands
Use the command `anacron`, commonly used options are:

| Options | Description |
| --- | --- |
| -f | Execute all jobs, ignoring timestamps |
| -u | Update the timestamp to the current time without performing any action |
| -T | Test the validity of the configuration file /etc/anacrontab |

For more help information, please [Browse the manual page](https://man7.org/linux/man-pages/man8/anacron.8.html)
116 changes: 116 additions & 0 deletions docs/guides/automation/anacron.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
title: anacron - 自动化命令
author: tianci li
contributors: Steven Spencer
update: 2021-10-20
---

# anacron- 定期运行命令

## 前提条件

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

## 假设

* 您已了解 bash、python 或其他脚本/编程工具的基本知识,并希望自动运行脚本。
* 您已以 root 用户身份登录,或者用`su - root`切换到root

## anacron简介
**anacron用于定期运行命令,运行频率以天为单位进行定义,它适用于在笔记本电脑和台式机等不会全天候7*24运行的计算机上。假设您有一个计划的任务(比如备份脚本)要在每天凌晨使用crontab运行,当您睡着的时候,此时您的台式机/笔记本电脑已经关闭。您的备份脚本将不会执行。但是,如果您使用anacron,您可以放心,下次再次打开台式机/笔记本电脑时,将执行备份脚本。**

anacron的出现并不是取代crontab,而是与crontab相互补充。它们的关系如下图:

![Relations](../images/anacron_01.png)

## anacron配置文件

```bash
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
```

首先查看默认的配置文件:
```bash
shell > cat /etc/anacontab
# /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
# 默认45分钟,指定anacron中每个作业的延迟将随机增加0到45分钟。
RANDOM_DELAY=45
# 指定工作的时间范围,这里表示3:00~22:00
START_HOURS_RANGE=3-22

# period in days delay in minutes job-identifier command
# 每天开机5分钟后就检查 /etc/cron.daily 目录内的文件是否被执行,如果今天没有被执行,那就执行下一个
1 5 cron.daily nice run-parts /etc/cron.daily
# 每隔7天开机后25分钟检查 /etc/cron.weekly 目录内的文件是否被执行,如果一周内没有被执行,就会执行下一个
7 25 cron.weekly nice run-parts /etc/cron.weekly
# 每隔一个月开机后 45 分钟检查 /etc/cron.monthly 目录内的文件是否被执行
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
```

**/etc/cron.hourly/** - 通过`journalctl -u crond.service`可以知道,放入到里面的文件其实是被crond.server调用,表示每小时的第一分钟后开始执行命令。如下所示:

```bash
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)

```

更多配置文件信息,请[浏览手册页](https://man7.org/linux/man-pages/man5/anacrontab.5.html)

## 用户使用
为了使某些文件在这些自动定义的时间内运行,您所需要做的就是将脚本文件复制到相关目录中,并确保其拥有**x执行权限(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的有效性|

更多帮助信息,请[浏览手册页](https://man7.org/linux/man-pages/man8/anacron.8.html)
6 changes: 4 additions & 2 deletions docs/guides/automation/cron_jobs_howto.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Automating With cron
title: cron - Automating commands
author: Steven Spencer
contributors: Ezequiel Bruni
---

# Automating Processes with cron and crontab
Expand Down Expand Up @@ -128,6 +130,6 @@ When determining when to run a script, you need to take time and plan it out, pa

# Conclusions

The cron/crontab system is a very powerful tool for the Rocky Linux desktop user or systems administrator. It can allow you to automate tasks and scripts so that you don't have to remember to run them manually.
The cron/crontab system is a very powerful tool for the Rocky Linux desktop user or systems administrator. It can allow you to automate tasks and scripts so that you don't have to remember to run them manually. For machines that are not on 24 hours a day, explore [anacron - Automating commands](anacron.md).

While the basics are pretty easy, you can get a lot more complex. For more information on crontab head up to the [crontab manual page](https://man7.org/linux/man-pages/man5/crontab.5.html). You can also simply do a web search for "crontab" which will give you a wealth of results to help you fine-tune your crontab skills.
20 changes: 13 additions & 7 deletions docs/guides/automation/cron_jobs_howto.zh.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
title: cron - 自动化命令
author: unknown
contributors: Steven Spencer
---

# 在 Rocky Linux 中使用 cron 和 crontab 自动化进程

## 准备工作
Expand Down Expand Up @@ -40,25 +46,25 @@ _crontab_ 本质上是一个列表,用户可以在其中添加自己的自动

```
# 编辑此文件以引入要由 cron 运行的任务。
#
#
# 一行定义一个要运行的任务
# 在一行中用不同的字段指示何时运行该任务
# 以及为该任务运行的命令
#
#
# 要定义时间,可以为
# 分钟(m)、小时(h)、月日(dom)、月(mon)和星期(dow)
# 提供具体值,或者在这些字段中使用“*”(表示“任何”)。
#
#
# 注意,将根据 cron 的系统守护程序的时间和时区概念启动任务。
#
#
# 作业的输出(包括错误)通过电子邮件发送给
# crontab 文件所属的用户(除非重定向)。
# cron
# 例如,每周一上午 5 点运行所有用户帐户的备份:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
#
# 有关更多信息,请参见 crontab(5)和 cron(8)的手册页
#
#
# m h dom mon dow command
```

Expand Down Expand Up @@ -121,6 +127,6 @@ _crontab_ 本质上是一个列表,用户可以在其中添加自己的自动

# 总结

对于 Rocky Linux 桌面用户或系统管理员而言,cron/crontab 系统是一个非常强大的工具。它可以让您自动执行任务和脚本,这样您就不必记住手动运行它们。
对于 Rocky Linux 桌面用户或系统管理员而言,cron/crontab 系统是一个非常强大的工具。它可以让您自动执行任务和脚本,这样您就不必记住手动运行它们。对于不是一天 24 小时运行的机器,探索 [anacron - 自动化命令](anacron.zh.md)。

虽然基础知识很简单,但实际任务可能很复杂。有关 crontab 的更多信息,请访问 [crontab 手册页](https://man7.org/linux/man-pages/man5/crontab.5.html)。您还可以简单地在网上搜索“crontab”,它将为您提供大量搜索结果,帮助您微调 crontab 表达式。
Binary file added docs/guides/images/anacron_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.