mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
翻译完成
This commit is contained in:
parent
bb9074ff4b
commit
4efd6b6d13
@ -1,207 +0,0 @@
|
||||
[#]: subject: (Scheduling tasks with cron)
|
||||
[#]: via: (https://fedoramagazine.org/scheduling-tasks-with-cron/)
|
||||
[#]: author: (Darshna Das https://fedoramagazine.org/author/climoiselle/)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (MjSeven)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
Scheduling tasks with cron
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Photo by [Yomex Owo][2] on [Unsplash][3]
|
||||
|
||||
Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called _cron_ jobs and are mostly used to automate system maintenance or administration tasks. For example, you could set a _cron_ job to automate repetitive tasks such as backing up database or data, updating the system with the latest security patches, checking the disk space usage, sending emails, and so on. The _cron_ jobs can be scheduled to run by the minute, hour, day of the month, month, day of the week, or any combination of these.
|
||||
|
||||
### **Some advantages of cron**
|
||||
|
||||
These are a few of the advantages of using _cron_ jobs:
|
||||
|
||||
* You have much more control over when your job runs i.e. you can control the minute, the hour, the day, etc. when it will execute.
|
||||
* It eliminates the need to write the code for the looping and logic of the task and you can shut it off when you no longer need to execute the job.
|
||||
* Jobs do not occupy your memory when not executing so you are able to save the memory allocation.
|
||||
* If a job fails to execute and exits for some reason it will run again when the proper time comes.
|
||||
|
||||
|
||||
|
||||
### Installing the cron daemon
|
||||
|
||||
Luckily Fedora Linux is pre-configured to run important system tasks to keep the system updated. There are several utilities that can run tasks such as _cron_, _anacron_, _at_ and _batch_. This article will focus on the installation of the _cron_ utility only. Cron is installed with the _cronie_ package that also provides the _cron_ services.
|
||||
|
||||
To determine if the package is already present or not, use the rpm command:
|
||||
|
||||
```
|
||||
$ rpm -q cronie
|
||||
Cronie-1.5.2-4.el8.x86_64
|
||||
```
|
||||
|
||||
If the _cronie_ package is installed it will return the full name of the _cronie_ package. If you do not have the package present in your system it will say the package is not installed.
|
||||
To install type this:
|
||||
|
||||
```
|
||||
$ dnf install cronie
|
||||
```
|
||||
|
||||
### Running the cron daemon
|
||||
|
||||
A _cron_ job is executed by the _crond_ service based on information from a configuration file. Before adding a job to the configuration file, however, it is necessary to start the _crond_ service, or in some cases install it. What is _crond_? _Crond_ is the compressed name of cron daemon (crond). To determine if the _crond_ service is running or not, type in the following command:
|
||||
|
||||
```
|
||||
$ systemctl status crond.service
|
||||
● crond.service - Command Scheduler
|
||||
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor pre>
|
||||
Active: active (running) since Sat 2021-03-20 14:12:35 PDT; 1 day 21h ago
|
||||
Main PID: 1110 (crond)
|
||||
```
|
||||
|
||||
If you do not see something similar including the line “Active: active (running) since…”, you will have to start the _crond_ daemon. To run the _crond_ service in the current session, enter the following command:
|
||||
|
||||
```
|
||||
$ systemctl run crond.service
|
||||
```
|
||||
|
||||
To configure the service to start automatically at boot time, type the following:
|
||||
|
||||
```
|
||||
$ systemctl enable crond.service
|
||||
```
|
||||
|
||||
If, for some reason, you wish to stop the _crond_ service from running, use the _stop_ command as follows:
|
||||
|
||||
```
|
||||
$ systemctl stop crond.service
|
||||
```
|
||||
|
||||
To restart it, simply use the _restart_ command:
|
||||
|
||||
```
|
||||
$ systemctl restart crond.service
|
||||
```
|
||||
|
||||
### Defining a cron job
|
||||
|
||||
#### The cron configuration
|
||||
|
||||
Here is an example of the configuration details for a _cron_ job. This defines a simple _cron_ job to pull the latest changes of a _git_ master branch into a cloned repository:
|
||||
|
||||
```
|
||||
*/59 * * * * username cd /home/username/project/design && git pull origin master
|
||||
```
|
||||
|
||||
There are two main parts:
|
||||
|
||||
* The first part is “*/59 * * * *”. This is where the timer is set to every 59 minutes.
|
||||
* The rest of the line is the command as it would run from the command line.
|
||||
The command itself in this example has three parts:
|
||||
* The job will run as the user “username”
|
||||
* It will change to the directory /home/username/project/design
|
||||
* The git command runs to pull the latest changes in the master branch.
|
||||
|
||||
|
||||
|
||||
#### **Timing syntax**
|
||||
|
||||
The timing information is the first part of the _cron_ job string, as mentioned above. This determines how often and when the cron job is going to run. It consists of 5 parts in this order:
|
||||
|
||||
* minute
|
||||
* hour
|
||||
* day of the month
|
||||
* month
|
||||
* day of the week
|
||||
|
||||
|
||||
|
||||
Here is a more graphic way to explain the syntax may be seen here:
|
||||
|
||||
```
|
||||
.---------------- minute (0 - 59)
|
||||
| .------------- hour (0 - 23)
|
||||
| | .---------- day of month (1 - 31)
|
||||
| | | .------- month (1 - 12) OR jan,feb,mar,apr …
|
||||
| | | | .---- day of week (0-6) (Sunday=0 or 7)
|
||||
| | | | | OR sun,mon,tue,wed,thr,fri,sat
|
||||
| | | | |
|
||||
* * * * user-name command-to-be-executed
|
||||
```
|
||||
|
||||
#### Use of the **asterisk**
|
||||
|
||||
An asterisk (*) may be used in place of a number to represents all possible values for that position. For example, an asterisk in the minute position would make it run every minute. The following examples may help to better understand the syntax.
|
||||
|
||||
This cron job will run every minute, all the time:
|
||||
|
||||
```
|
||||
* * * * [command]
|
||||
```
|
||||
|
||||
A slash (/) indicates a multiple number of minutes The following example will run 12 times per hour, i.e., every 5 minutes:
|
||||
|
||||
```
|
||||
*/5 * * * * [command]
|
||||
```
|
||||
|
||||
The next example will run once a month, on the second day of the month at midnight (e.g. January 2nd 12:00am, February 2nd 12:00am, etc.):
|
||||
|
||||
```
|
||||
0 0 2 * * [command]
|
||||
```
|
||||
|
||||
#### Using crontab to create a cron job
|
||||
|
||||
Cron jobs run in the background and constantly check the _/etc/crontab_ file, and the _/etc/cron.*/_ and _/var/spool/cron/_ directories. Each user has a unique crontab file in _/var/spool/cron/_ .
|
||||
|
||||
These _cron_ files are not supposed to be edited directly. The _crontab_ command is the method you use to create, edit, install, uninstall, and list cron jobs.
|
||||
|
||||
The same _crontab_ command is used for creating and editing cron jobs. And what’s even cooler is that you don’t need to restart cron after creating new files or editing existing ones.
|
||||
|
||||
```
|
||||
$ crontab -e
|
||||
```
|
||||
|
||||
This opens your existing _crontab_ file or creates one if necessary. The _vi_ editor opens by default when calling _crontab -e_. Note: To edit the _crontab_ file using Nano editor, you can optionally set the **EDITOR**=nano environment variable.
|
||||
|
||||
List all your _cron_ jobs using the option _-l_ and specify a user using the _-u_ option, if desired.
|
||||
|
||||
```
|
||||
$ crontab -l
|
||||
$ crontab -u username -l
|
||||
```
|
||||
|
||||
Remove or erase all your _cron_ jobs using the following command:
|
||||
|
||||
```
|
||||
$ crontab -r
|
||||
```
|
||||
|
||||
To remove jobs for a specific user you must run the following command as the _root user_:
|
||||
|
||||
```
|
||||
$ crontab -r -u username
|
||||
```
|
||||
|
||||
Thank you for reading. _cron_ jobs may seem like a tool just for system admins, but they are actually relevant to many kinds of web applications and user tasks.
|
||||
|
||||
#### Reference
|
||||
|
||||
Fedora Linux documentation for [Automated Tasks][4]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/scheduling-tasks-with-cron/
|
||||
|
||||
作者:[Darshna Das][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/climoiselle/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/03/schedule_with_cron-816x345.jpg
|
||||
[2]: https://unsplash.com/@yomex4life?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[3]: https://unsplash.com/s/photos/clock?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[4]: https://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-autotasks.html
|
202
translated/tech/20210412 Scheduling tasks with cron.md
Normal file
202
translated/tech/20210412 Scheduling tasks with cron.md
Normal file
@ -0,0 +1,202 @@
|
||||
[#]: subject: "Scheduling tasks with cron"
|
||||
[#]: via: "https://fedoramagazine.org/scheduling-tasks-with-cron/"
|
||||
[#]: author: "Darshna Das https://fedoramagazine.org/author/climoiselle/"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "MjSeven"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
使用 cron 调度任务
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Photo by [Yomex Owo][2] on [Unsplash][3]
|
||||
|
||||
Cron 是一个调度守护进程,它以指定的时间间隔执行任务,这些任务称为 _corn_ 作业,主要用于自动执行系统维护或管理任务。例如,你可以设置一个 _cron_ 作业来自动执行重复的任务,比如备份数据库或数据,使用最新的安全补丁更新系统,检查磁盘空间使用情况,发送电子邮件等等。 _cron_ 作业可以按分钟、小时、日、月、星期或它们的任意组合运行。
|
||||
|
||||
### **cron 的一些优点**
|
||||
|
||||
以下是使用 _cron_ 作业的一些优点:
|
||||
|
||||
* 你可以更好地控制作业的运行时间。例如,你可以精确到分钟、小时、天等。
|
||||
* 它消除了为循环任务逻辑而去写代码的需要,当你不再需要执行任务时,可以直接关闭它。
|
||||
* 作业在不执行时不会占用内存,因此你可以节省内存分配。
|
||||
* 如果一个作业执行失败并由于某种原因退出,它将在指定的时间再次运行。
|
||||
|
||||
### 安装 cron 守护进程
|
||||
|
||||
幸运的是,Fedora Linux 预先配置了运行重要的系统任务来保持系统更新,有几个实用程序可以运行任务例如 _cron_、_anacorn_、_at_ 和 _batch_ 。本文只关注 _cron_ 实用程序的安装。Cron 和 _cronie_ 包一起安装,cronie 包也提供 _cron_ 服务。
|
||||
|
||||
要确定软件包是否已经存在,使用 rpm 命令:
|
||||
|
||||
```bash
|
||||
$ rpm -q cronie
|
||||
Cronie-1.5.2-4.el8.x86_64
|
||||
```
|
||||
|
||||
如果安装了 _cronie_ ,它将返回 _cronie_ 包的全名。如果你的系统中没有安装,则会显示未安装。
|
||||
|
||||
使用以下命令安装:
|
||||
|
||||
```bash
|
||||
$ dnf install cronie
|
||||
```
|
||||
|
||||
### 运行 cron 守护进程
|
||||
|
||||
一个 _cron_ 作业由 _crond_ 服务来执行,它会读取配置文件中的信息。在将作业添加到配置文件之前,必须启动 _crond_ 服务,或者安装它。什么是 _crond_ 呢?_Crond_ 是 cron 守护程序的简称。要确定 _crond_ 服务是否正在运行,输入以下命令:
|
||||
|
||||
```bash
|
||||
$ systemctl status crond.service
|
||||
● crond.service - Command Scheduler
|
||||
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor pre>
|
||||
Active: active (running) since Sat 2021-03-20 14:12:35 PDT; 1 day 21h ago
|
||||
Main PID: 1110 (crond)
|
||||
```
|
||||
|
||||
如果你没有看到类似的内容 "Active: active (running) since…",你需要启动 _crond_ 守护进程。要在当前会话中运行 _crond_ 服务,输入以下命令:
|
||||
|
||||
```bash
|
||||
$ systemctl run crond.service
|
||||
```
|
||||
|
||||
将其配置为开机自启动,输入以下命令:
|
||||
|
||||
```bash
|
||||
$ systemctl enable crond.service
|
||||
```
|
||||
|
||||
如果出于某种原因,你希望停止 _crond_ 服务,按以下方式使用 _stop_ 命令:
|
||||
|
||||
```bash
|
||||
$ systemctl stop crond.service
|
||||
```
|
||||
|
||||
要重新启动它,只需使用 _restart_ 命令:
|
||||
|
||||
```bash
|
||||
$ systemctl restart crond.service
|
||||
```
|
||||
|
||||
### **定义 cron 工作**
|
||||
|
||||
#### **cron 配置**
|
||||
|
||||
以下是一个 _cron_ 作业的配置细节示例。它定义了一个简单的 _cron_ 作业,将 _git_ master 分支的最新更改拉取到克隆的仓库中:
|
||||
|
||||
```shell
|
||||
*/59 * * * * username cd /home/username/project/design && git pull origin master
|
||||
```
|
||||
|
||||
主要有两部分:
|
||||
|
||||
* 第一部分是 “*/59 * * * *”。这表明计时器设置为每 59 分钟一次。
|
||||
* 该行的其余部分是命令,因为它将从命令行运行。
|
||||
在此示例中,命令本身包含三个部分:
|
||||
* 作业将以用户 ”username“ 的身份运行
|
||||
* 它将切换到目录 `/home/username/project/design`
|
||||
* 运行 git 命令拉取 master 分支中的最新更改
|
||||
|
||||
#### **时间语法**
|
||||
|
||||
如上所述,时间信息是 _cron_ 作业字符串的第一部分,如上所属。它决定了 cron 作业运行的频率和时间。它按以下顺序包括 5 个部分:
|
||||
|
||||
* 分钟
|
||||
* 小时
|
||||
* 一个月中的某天
|
||||
* 月份
|
||||
* 一周中的某天
|
||||
|
||||
下面是一种更图形化的方式来解释语法:
|
||||
|
||||
```bash
|
||||
.---------------- 分钟 (0 - 59)
|
||||
| .------------- 小时 (0 - 23)
|
||||
| | .---------- 一月中的某天 (1 - 31)
|
||||
| | | .------- 月份 (1 - 12) 或 jan,feb,mar,apr …
|
||||
| | | | .---- 一周中的某天 (0-6) (Sunday=0 or 7)
|
||||
| | | | | 或 sun,mon,tue,wed,thr,fri,sat
|
||||
| | | | |
|
||||
* * * * * user-name command-to-be-executed
|
||||
```
|
||||
|
||||
#### **星号**的使用
|
||||
|
||||
星号(*)可以用来替代数字,表示该位置的所有可能值。例如,分钟位置上的星号会使它每分钟运行一次。以下示例可能有助于更好地理解语法。
|
||||
|
||||
这个 cron 作业将每分钟运行一次:
|
||||
|
||||
```bash
|
||||
* * * * [command]
|
||||
```
|
||||
|
||||
斜杠表示分钟数。下面的示例将每小时运行 12 次,即每 5 分钟运行一次:
|
||||
|
||||
```bash
|
||||
*/5 * * * * [command]
|
||||
```
|
||||
|
||||
下一个示例将每月的第二天午夜(例如 1 月 2 日凌晨 12:00,2 月 2 日凌晨 12:00 等等):
|
||||
|
||||
```bash
|
||||
0 0 2 * * [command]
|
||||
```
|
||||
|
||||
#### 使用 crontab 创建一个 cron 作业
|
||||
|
||||
Cron 作业会在后台运行,它会不断检查 _/etc/crontab_ 文件和 _/etc/cron.*/_ 以及 _/var/spool/cron/_ 目录。每个用户在 _/var/spool/cron/_ 中都有一个唯一的 crontab 文件。
|
||||
|
||||
不应该直接编辑这些 _cron_ 文件。_crontab_ 命令是用于创建、编辑、安装、卸载和列出 cron 作业的方法。
|
||||
|
||||
更酷的是,在创建新文件或编辑现有文件后,你无需重新启动 cron。
|
||||
|
||||
```bash
|
||||
$ crontab -e
|
||||
```
|
||||
|
||||
这将打开你现有的 _crontab_ 文件,或者创建一个。调用 _crontab -e_ 时,默认情况下会使用 _vi_ 编辑器。注意:使用 Nano 编辑 _crontab_ 文件,可以选择设置 **EDITOR**=nano 环境变量。
|
||||
|
||||
使用 -l 选项列出所有 cron 作业。如果需要,使用 -u 选项指定一个用户。
|
||||
|
||||
```bash
|
||||
$ crontab -l
|
||||
$ crontab -u username -l
|
||||
```
|
||||
|
||||
使用以下命令删除所有 _cron_ 作业:
|
||||
|
||||
```bash
|
||||
$ crontab -r
|
||||
```
|
||||
|
||||
要删除特定用户的作业,你必须以 _root 用户_ 身份运行以下命令:
|
||||
|
||||
```bash
|
||||
$ crontab -r -u username
|
||||
```
|
||||
|
||||
感谢你的阅读。_cron_ 作业看起来可能只是系统管理员的工具,但它实际上与许多 Web 应用程序和用户任务有关。
|
||||
|
||||
#### 参考
|
||||
|
||||
Fedora Linux 文档的[自动化任务][4]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/scheduling-tasks-with-cron/
|
||||
|
||||
作者:[Darshna Das][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[MjSeven](https://github.com/MjSeven)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/climoiselle/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2021/03/schedule_with_cron-816x345.jpg
|
||||
[2]: https://unsplash.com/@yomex4life?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[3]: https://unsplash.com/s/photos/clock?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
|
||||
[4]: https://docs.fedoraproject.org/en-US/Fedora/12/html/Deployment_Guide/ch-autotasks.html
|
Loading…
Reference in New Issue
Block a user