-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# 定时任务设定 | ||
|
||
## 分情况使用 | ||
|
||
### 1.后端使用 | ||
|
||
`springboot`自带`@Scheduled`注解,就是用来执行定时任务的,根据传入的cron表达式定时执行添加注解的函数。 | ||
|
||
### 2.Linux服务器使用 | ||
|
||
在linux上使用`crontab`来执行定时任务。 | ||
|
||
查看当前Linux系统中的定时任务 | ||
```shell | ||
crontab -l | ||
``` | ||
|
||
编辑当前Linux系统中的定时任务 | ||
```shell | ||
crontab -e | ||
``` | ||
|
||
一般定时任务设定格式: | ||
|
||
`[cron表达式]` + `[用户身份]` + `[执行命令]` | ||
|
||
示例:0 0,12 * * * sudo -u root /sbin/hwclock --hctosys(每天0点与12点使用root用户执行系统时间与硬件时间同步任务) | ||
|
||
## Cron表达式 | ||
|
||
cron 表达式是一个字符串,该字符串由 6 个空格分为 7 个域,每一个域代表一个时间含义。 格式如下: | ||
|
||
``` | ||
[秒] [分] [时] [日] [月] [周] [年] | ||
``` | ||
通常定义 “年” 的部分可以省略,实际常用的由 前六部分组成 | ||
|
||
### Cron 的各个域的定义 | ||
|
||
| 域 | 是否必填 | 值以及范围 | 通配符 | | ||
| ----------- | ----------- | ----------- | ----------- | | ||
| 秒 | 是 |0-59 | , - * / | | ||
| 分 | 是 |0-59 | , - * / | | ||
| 时 | 是 |0-23 | , - * / | | ||
| 日 | 是 |1-31 | , - * ? / L W | | ||
| 月 | 是 |1-12 或 JAN-DEC | , - * / | | ||
| 周 | 是 |1-7 或 SUN-SAT | , - * ? / L # | | ||
| 年 | 否 |1970-2099 | , - * / | | ||
|
||
|
||
数字:表示对应的时间点值(如1表示1点) | ||
|
||
星号(*):表示该字段的所有可能值,如分钟字段中的星号表示每分钟的任何时间点 | ||
|
||
逗号(,):用于分割字段中的多个取值,如“2,5,10”表示取值为2、5、10的时间点 | ||
|
||
连接符(-):用于指定时间段,如“1-5”表示取值范围为1到5的所有时间点 | ||
|
||
斜杠(/):用于指定时间步长,如“*/10”表示每隔10个时间点执行一次任务 | ||
|
||
|
||
|
||
### 示例 | ||
|
||
每隔1分钟执行一次:`0 */1 * * * ?` | ||
|
||
每天22点执行一次:`0 0 22 * * ?` | ||
|
||
每月1号凌晨1点执行一次:`0 0 1 1 * ?` | ||
|
||
每月最后一天23点执行一次:`0 0 23 L * ?` | ||
|
||
每周周六凌晨3点实行一次:`0 0 3 ? * L` | ||
|
||
在24分、30分执行一次:`0 24,30 * * * ?` |