This commit is contained in:
Xingyu Wang 2019-11-12 23:31:51 +08:00
parent cdeb7498f9
commit e3a8169735
2 changed files with 290 additions and 241 deletions

View File

@ -1,241 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Schedule and Automate tasks in Linux using Cron Jobs)
[#]: via: (https://www.linuxtechi.com/schedule-automate-tasks-linux-cron-jobs/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
How to Schedule and Automate tasks in Linux using Cron Jobs
======
Sometimes, you may have tasks that need to be performed on a regular basis or at certain predefined intervals. Such tasks include backing up databases, updating the system, performing periodic reboots and so on. Such tasks are referred to as **cron jobs**. Cron jobs are used for **automation of tasks** that come in handy and help in simplifying the execution of repetitive and sometimes mundane tasks. **Cron** is a daemon that allows you to schedule these jobs which are then carried out at specified intervals. In this tutorial, you will learn how to schedule jobs using cron jobs.
[![Schedule -tasks-in-Linux-using cron][1]][2]
### The Crontab file
A crontab file, also known as a **cron table**, is a simple text file that contains rules or commands that specify the time interval of execution of a task. There are two categories of crontab files:
**1)  System-wide crontab file**
These are usually used by Linux services & critical applications requiring root privileges. The system crontab file is located at **/etc/crontab** and can only be accessed and edited by the root user. Its usually used for the configuration of system-wide daemons. The crontab file looks as shown:
[![etc-crontab-linux][1]][3]
**2) User-created crontab files**
Linux users can also create their own cron jobs with the help of the crontab command. The cron jobs created will run as the user who created them.
All cron jobs are stored in /var/spool/cron (For RHEL and CentOS distros) and /var/spool/cron/crontabs (For Debian and Ubuntu distros), the cron jobs are listed using the username of the user that created the cron job
The **cron daemon** runs silently in the background checking the **/etc/crontab** file and **/var/spool/cron** and **/etc/cron.d*/** directories
The **crontab** command is used for editing cron files. Let us take a look at the anatomy of a crontab file.
### The anatomy of a crontab file
Before we go further, its important that we first explore how a crontab file looks like. The basic syntax for a crontab file comprises 5 columns represented by asterisks followed by the command to be carried out.
*    *    *    *    *    command
This format can also be represented as shown below:
m h d moy dow command
OR
m h d moy dow /path/to/script
Lets expound on each entry
* **m**: This represents minutes. Its specified from 0 to 59
* **h**: This denoted the hour specified from 0 to 23
* **d**:  This represents the day of the month. Specified between 1 to 31`
* **moy**: This is the month of the year. Its specified between 1 to 12
* **doy**:  This is the day of the week. Its specified between 0 and 6 where 0 = Sunday
* **Command**: This is the command to be executed e.g backup command, reboot, & copy
### Managing cron jobs
Having looked at the architecture of a crontab file, lets see how you can create, edit and delete cron jobs
**Creating cron jobs**
To create or edit a cron job as the root user, run the command
# crontab -e
To create a cron job or schedule a task as another user, use the syntax
# crontab -u username -e
For instance, to run a cron job as user Pradeep, issue the command:
# crontab -u Pradeep -e
If there is no preexisting crontab file, then you will get a blank text document. If a crontab file was existing, The  -e option allows  to edit the file,
**Listing crontab files**
To view the cron jobs that have been created, simply pass the -l option as shown
# crontab -l
**Deleting a  crontab file**
To delete a cron file, simply run crontab -e and delete or the line of the cron job that you want and save the file.
To remove all cron jobs, run the command:
# crontab -r
That said, lets have a look at different ways that you can schedule tasks
### Crontab examples in Scheduling tasks.
All cron jobs being with a shebang header as shown
#!/bin/bash
This indicates the shell you are using, which, for this case, is bash shell.
Next, specify the interval at which you want to schedule the tasks using the cron job entries we specified earlier on.
To reboot a system daily at 12:30 pm, use the syntax:
30  12 *  *  * /sbin/reboot
To schedule the reboot at 4:00 am use the syntax:
0  4  *  *  *  /sbin/reboot
**NOTE:**  The asterisk * is used to match all records
To run a script twice every day, for example, 4:00 am and 4:00 pm, use the syntax.
0  4,16  *  *  *  /path/to/script
To schedule a cron job to run every Friday at 5:00 pm  use the syntax:
0  17  *  *  Fri  /path/to/script
OR
0 17  *  *  *  5  /path/to/script
If you wish to run your cron job every 30 minutes then use:
*/30  *  *  *  * /path/to/script
To schedule cron to run after every 5 hours, run
*  */5  *  *  *  /path/to/script
To run a script on selected days, for example, Wednesday and Friday at 6.00 pm execute:
0  18  *  *  wed,fri  /path/to/script
To schedule multiple tasks to use a single cron job, separate the tasks using a semicolon for example:
*  *  *  *  *  /path/to/script1 ; /path/to/script2
### Using special strings to save time on writing cron jobs
Some of the cron jobs can easily be configured using special strings that correspond to certain time intervals. For example,
1)  @hourly timestamp corresponds to  0 * * * *
It will execute a task in the first minute of every hour.
@hourly /path/to/script
2) @daily timestamp is equivalent to  0 0 * * *
It executes a task in the first minute of every day (midnight). It comes in handy when executing daily jobs.
  @daily /path/to/script
3) @weekly   timestamp is the equivalent to  0 0 1 * mon
It executes a cron job in the first minute of every week where a week whereby, a  week starts on Monday.
 @weekly /path/to/script
3) @monthly is similar to the entry 0 0 1 * *
It carries out a task in the first minute of the first day of the month.
  @monthly /path/to/script
4) @yearly corresponds to 0 0 1 1 *
It executes a task in the first minute of every year and is useful in sending New year greetings 🙂
@monthly /path/to/script
### Crontab Restrictions
As a Linux user, you can control who has the right to use the crontab command. This is possible using the **/etc/cron.deny** and **/etc/cron.allow** file. By default, only the /etc/cron.deny file exists and does not contain any entries. To restrict a user from using the crontab utility, simply add a users username to the file. When a user is added to this file, and the user tries to run the crontab command, he/she will encounter the error below.
![restricted-cron-user][1]
To allow the user to continue using the crontab utility,  simply remove the username from the /etc/cron.deny file.
If /etc/cron.allow file is present, then only the users listed in the file can access and use the crontab utility.
If neither file exists, then only the root user will have privileges to use the crontab command.
### Backing up crontab entries
Its always advised to backup your crontab entries. To do so, use the syntax
# crontab -l > /path/to/file.txt
For example,
```
# crontab -l > /home/james/backup.txt
```
**Checking cron logs**
Cron logs are stored in /var/log/cron file. To view the cron logs run the command:
```
# cat /var/log/cron
```
![view-cron-log-files-linux][1]
To view live logs, use the tail command as shown:
```
# tail -f /var/log/cron
```
![view-live-cron-logs][1]
**Conclusion**
In this guide, you learned how to create cron jobs to automate repetitive tasks, how to backup as well as how to view cron logs. We hope that this article provided useful insights with regard to cron jobs. Please dont hesitate to share your feedback and comments.
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/schedule-automate-tasks-linux-cron-jobs/
作者:[Pradeep Kumar][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://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/11/Schedule-tasks-in-Linux-using-cron.jpg
[3]: https://www.linuxtechi.com/wp-content/uploads/2019/11/etc-crontab-linux.png

View File

@ -0,0 +1,290 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to Schedule and Automate tasks in Linux using Cron Jobs)
[#]: via: (https://www.linuxtechi.com/schedule-automate-tasks-linux-cron-jobs/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
如何使用 cron 任务在 Linux 中计划和自动化任务
======
有时,你可能需要定期执行任务或以预定的时间间隔执行任务。这些任务包括备份数据库、更新系统、执行定期重新引导等。这些任务称为 “cron 任务”。cron 任务用于“自动执行的任务”它有助于简化重复的、有时是乏味的任务的执行。cron 是一个守护进程,可让你调度这些任务,然后按指定的时间间隔执行这些任务。在本教程中,你将学习如何使用 cron 来调度任务。
![Schedule -tasks-in-Linux-using cron][2]
### crontab 文件
crontab 即 “cron table”是一个简单的文本文件其中包含指定任务执行时间间隔的规则或命令。 crontab 文件分为两类:
1系统范围的 crontab 文件
这些通常由需要 root 特权的 Linux 服务及关键应用程序使用。系统 crontab 文件位于 `/etc/crontab` 中,并且只能由 root 用户访问和编辑。通常用于配置系统范围的守护程序。`crontab` 文件的看起来类似如下所示:
![etc-crontab-linux][3]
2用户创建的 crontab 文件
Linux 用户还可以在 `crontab` 命令的帮助下创建自己的 cron 任务。创建的 cron 任务将以创建它们的用户身份运行。
所有 cron 任务都存储在 `/var/spool/cron`(对于 RHEL 和 CentOS 发行版)和 `/var/spool/cron/crontabs`(对于 Debian 和 Ubuntu 发行版cron 任务使用创建该文件的用户的用户名列出。
cron 守护进程在后台静默地检查 `/etc/crontab` 文件和 `/var/spool/cron``/etc/cron.d*/` 目录。
`crontab` 命令用于编辑 cron 文件。让我们看一下 crontab 文件的结构。
### crontab 文件剖析
在继续之前,我们要首先探索 crontab 文件的格式。crontab 文件的基本语法包括 5 列,由星号表示,后跟要执行的命令。
```
*    *    *    *    *    command
```
此格式也可以表示如下:
```
m h d moy dow command
```
```
m h d moy dow /path/to/script
```
让我们来解释一下每个条目
* `m`:代表分钟。范围是 0 到 59
* `h`:表示小时,范围是 0 到 23
* `d`:代表一个月中的某天,范围是 1 到 31
* `moy`:这是一年中的月份。范围是 1 到 12
* `doy`:这是星期几。范围是 0 到 6其中 0 代表星期日
* `Command`:这是要执行的命令,例如备份命令、重新启动和复制命令等
### 管理 cron 任务
看完 crontab 文件的结构之后,让我们看看如何创建、编辑和删除 cron 任务。
#### 创建 cron 任务
要以 root 用户身份创建或编辑 cron 任务,请运行以下命令:
```
# crontab -e
```
要为另一个用户创建或安排 cron 任务,请使用以下语法:
```
# crontab -u username -e
```
例如,要以 Pradeep 用户身份运行 cron 任务,请发出以下命令:
```
# crontab -u Pradeep -e
```
如果该 crontab 文件尚不存在,那么你将打开一个空白文本文档。如果该 crontab 文件已经存在,则 `-e` 选项会让你编辑该文件,
#### 列出 crontab 文件
要查看已创建的 cron 任务,只需传递 `-l` 选项:
```
# crontab -l
```
#### 删除 crontab 文件
要删除 cron 任务,只需运行 `crontab -e` 并删除所需的 cron 任务行,然后保存该文件。
要删除所有的 cron 任务,请运行以下命令:
```
# crontab -r
```
然后,让我们看一下安排任务的不同方式。
### crontab 安排任务示例
如图所示,所有 cron 任务文件都带有释伴标头。
```
#!/bin/bash
```
这表示你正在使用的 shell在这种情况下即 bash shell。
接下来,使用我们之前指定的 cron 任务条目指定要安排任务的时间间隔。
要每天下午 12:30 重新引导系统,请使用以下语法:
```
30  12 *  *  * /sbin/reboot
```
要安排在凌晨 4:00 重启,请使用以下语法:
```
0  4  *  *  *  /sbin/reboot
```
注:星号 `*` 用于匹配所有记录。
要每天两次运行脚本(例如,凌晨 4:00 和下午 4:00请使用以下语法
```
0  4,16  *  *  *  /path/to/script
```
要安排 cron 任务在每个星期五下午 5:00 运行,请使用以下语法:
```
0  17  *  *  Fri  /path/to/script
```
```
0 17  *  *  *  5  /path/to/script
```
如果你希望每 30 分钟运行一次 cron 任务,请使用:
```
*/30  *  *  *  * /path/to/script
```
要安排 cron 任务每 5 小时运行一次,请运行:
```
*  */5  *  *  *  /path/to/script
```
要在选定的日期(例如,星期三和星期五的下午 6:00运行脚本请执行以下操作
```
0  18  *  *  wed,fri  /path/to/script
```
要使用单个 cron 任务运行多个命令,请使用分号分隔任务,例如:
```
*  *  *  *  *  /path/to/script1 ; /path/to/script2
```
### 使用特殊字符串节省编写 cron 任务的时间
某些 cron 任务可以使用对应于特定时间间隔的特殊字符串轻松配置。例如,
1`@hourly` 时间戳等效于 `0 * * * *`
它将在每小时的第一分钟执行一次任务。
```
@hourly /path/to/script
```
2`@daily` 时间戳等效于 `0 0 * * *`
它在每天的第一分钟(午夜)执行任务。它可以在执行日常工作时派上用场。
```
@daily /path/to/script
```
3`@weekly` 时间戳等效于 `0 0 1 * mon`
它在每周的第一分钟执行 cron 任务,一周是从星期一开始的。
```
@weekly /path/to/script
```
3`@monthly` 时间戳等效于 `0 0 1 * *`
它在每月第一天的第一分钟执行任务。
```
@monthly /path/to/script
```
4`@yearly` 时间戳等效于 `0 0 1 1 *`
它在每年的第一分钟执行任务,并且对发送新年问候很有用。
```
@yearly /path/to/script
```
### 限制 crontab
作为 Linux 用户,你可以控制谁有权使用 `crontab` 命令。可以使用 `/etc/cron.deny``/etc/cron.allow` 文件来控制。默认情况下,只有一个 `/etc/cron.deny` 文件,并且不包含任何条目。要限制用户使用 `crontab` 实用程序,只需将用户的用户名添加到文件中即可。当用户添加到该文件中,并且该用户尝试运行 `crontab` 命令时,他/她将遇到以下错误。
![restricted-cron-user][4]
要允许用户继续使用 `crontab` 实用程序,只需从 `/etc/cron.deny` 文件中删除用户名即可。
如果存在 `/etc/cron.allow` 文件,则仅文件中列出的用户可以访问和使用 `crontab` 实用程序。
如果两个文件都不存在,则只有 root 用户具有使用 `crontab` 命令的特权。
### 备份 crontab 条目
始终建议你备份 crontab 条目。为此,请使用语法
```
# crontab -l > /path/to/file.txt
```
例如:
```
# crontab -l > /home/james/backup.txt
```
### 检查 cron 日志
cron 日志存储在 `/var/log/cron` 文件中。要查看 cron 日志,请运行以下命令:
```
# cat /var/log/cron
```
![view-cron-log-files-linux][5]
要实时查看日志,请使用 `tail` 命令,如下所示:
```
# tail -f /var/log/cron
```
![view-live-cron-logs][6]
### 总结
在本指南中,你学习了如何创建 cron 任务以自动执行重复性任务,如何备份和查看 cron 日志。我们希望本文提供有关 cron 作业的有用见解。请随时分享你的反馈和意见。
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/schedule-automate-tasks-linux-cron-jobs/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/pradeep/
[b]: https://github.com/lujun9972
[1]: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
[2]: https://www.linuxtechi.com/wp-content/uploads/2019/11/Schedule-tasks-in-Linux-using-cron.jpg
[3]: https://www.linuxtechi.com/wp-content/uploads/2019/11/etc-crontab-linux.png
[4]: https://www.linuxtechi.com/wp-content/uploads/2019/11/restricted-cron-user.png
[5]: https://www.linuxtechi.com/wp-content/uploads/2019/11/view-cron-log-files-linux.png
[6]: https://www.linuxtechi.com/wp-content/uploads/2019/11/view-live-cron-logs.png