当前位置:网站首页>Cron表达式介绍
Cron表达式介绍
2022-07-03 02:44:00 【好奇的菜鸟】
一、什么是Cron表达式
cron 表达式是一个字符串,该字符串由 6 个空格分为 7 个域,每一个域代表一个时间含义。常用语定时任务。
二、Cron的格式
Cron有如下两种语法格式
Seconds Minutes Hours DayofMonth Month DayofWeek
Seconds Minutes Hours DayofMonth Month DayofWeek Year
通常定义 “年” 的部分可以省略,实际常用的由 前六部分组成
关于 cron 的各个域的定义如下表格所示:
| 域 | 是否必填 | 值以及范围 | 通配符 |
|---|---|---|---|
| 秒 | 是 | 0-59 | , - * / |
| 分 | 是 | 0-59 | , - * / |
| 时 | 是 | 0-23 | , - * / |
| 日 | 是 | 1-31 | , - * ? / L W |
| 月 | 是 | 1-12 或 JAN-DEC | , - * / |
| 周 | 是 | 1-7 或 SUN-SAT | , - * ? / L # |
| 年 | 否 | 1970-2099 | , - * / |
通配符含义介绍:
“ , ” 这里指的是在两个以上的时间点中都执行,如果我们在 “分” 这个域中定义为 8,12,35 ,则表示分别在第8分,第12分 第35分执行该定时任务;
“ - ” 这个比较好理解就是指定在某个域的连续范围,如果我们在 “时” 这个域中定义 1-6,则表示在1到6点之间每小时都触发一次,用 , 表示 1,2,3,4,5,6;
“ * ” 表示所有值,可解读为 “每”。 如果在“日”这个域中设置 *,表示每一天都会触发;
“ ? ” 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的8号触发一个操作,但不关心是周几,我们可以这么设置 0 0 0 8 * ?;
“ / ”在某个域上周期性触发,该符号将其所在域中的表达式分为两个部分,其中第一部分是起始值,除了秒以外都会降低一个单位,比如 在 “秒” 上定义 5/10 表示从 第 5 秒开始 每 10 秒执行一次,而在 “分” 上则表示从 第 5 秒开始 每 10 分钟执行一次;
“ L ”表示英文中的LAST 的意思,只能在 “日”和“周”中使用。在“日”中设置,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年), 在“周”上表示周六,相当于”7”或”SAT”。如果在”L”前加上数字,则表示该数据的最后一个。例如在“周”上设置”7L”这样的格式,则表示“本月最后一个周六”;
“ W ” 表示离指定日期的最近那个工作日(周一至周五)触发,只能在 “日” 中使用且只能用在具体的数字之后。若在“日”上置”15W”,表示离每月15号最近的那个工作日触发。假如15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果是 “1W” 就只能往本月的下一个最近的工作日推不能跨月往上一个月推;
“ # ” 表示每月的第几个周几,只能作用于 “周” 上。例如 ”2#3” 表示在每月的第三个周二。
三、常用表达式例子
(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
(7)0 0 12 * * ? 每天中午12点触发
(8)0 15 10 ? * * 每天上午10:15触发
(9)0 15 10 * * ? 每天上午10:15触发
(10)0 15 10 * * ? 每天上午10:15触发
(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
(18)0 15 10 15 * ? 每月15日上午10:15触发
(19)0 15 10 L * ? 每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
(23)0/2 * * * * ? 表示每2秒 执行任务
(24)0 0/2 * * * ? 表示每2分钟 执行任务
spring框架中,Cron表达式使用5个空格分割成6个域;日和周可能会冲突,如果在乎日,周使用“?”;如果在乎周,日使用“?”。
边栏推荐
- 搭建私有云盘 cloudreve
- Classes and objects - initialization and cleanup of objects - constructor call rules
- As a leader, how to control the code version and demand development when the epidemic comes| Community essay solicitation
- A2L file parsing based on CAN bus (2)
- How to change the panet layer in yolov5 to bifpn
- where 1=1 是什么意思
- Monitoring and management of JVM
- Process the dataset and use labelencoder to convert all IDs to start from 0
- Gbase 8C system table PG_ conversion
- The left value and the right finger explain better
猜你喜欢

Classes and objects - initialization and cleanup of objects - constructor call rules

定了,就选它
![[hcia]no.15 communication between VLANs](/img/59/a467c5920cbccb72040f39f719d701.jpg)
[hcia]no.15 communication between VLANs

Choose it when you decide

sql server数据库添加 mdf数据库文件,遇到的报错

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers

MATLAB小技巧(24)RBF,GRNN,PNN-神经网络

HTB-Devel

Sqlserver row to column pivot

Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
随机推荐
左值右指解释的比较好的
Gbase 8C function / stored procedure parameters (II)
Gbase 8C system table PG_ collation
Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
HTB-Devel
GBase 8c系统表-pg_class
SQL server queries the table structure of the specified table
Concrete CMS vulnerability
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
Pytest (6) -fixture (Firmware)
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
SQL statement
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
Cvpr2022 remove rain and fog
Add automatic model generation function to hade
处理数据集,使用LabelEncoder将所有id转换为从0开始
The core idea of performance optimization, dry goods sharing
Can netstat still play like this?