当前位置:网站首页>[Monday commuter radio station] cron expression. It's enough to read this article
[Monday commuter radio station] cron expression. It's enough to read this article
2022-06-11 20:32:00 【Yijun】
Hello, Hello everyone , Here is Yijun @ Monday commuter radio channel .
0. After reading this article, you will learn
- What is? cron expression
- cron The structure and rules of expressions
- cron The expression in Spring as well as Linux The use of
1. What is? cron expression ?
Example : 10 10 10 ? * MON-WED cron An expression is a string expression used to control time .
cron Expressions are generally composed of 5 A or 6 Space space , form 6 A or 7 Domains .
1.1 cron Structure of expression
cron The structure of the expression is as follows .
Seconds Minutes Hours DayOfMonth Month DayOfWeek Year(Year Omission )
From left to right ,cron The fields of the expression are expressed as second 、 branch 、 when 、 Date in month 、 month 、 Date of the week 、 year .
For the convenience of memory , We can write it down as “ Seconds minutes ten ( Ten links ) Japan ( Different from the following Sunday ), July Sunday ( Sunday is the day of the week ) year ”—— Ten suns were quickly divided , The moon is a year every Sunday .
The author also suddenly thought of this method , Because I remember it several times before, but it is still vague . If readers have a better way to remember, they might as well leave a message in the comment area .
1.2 cron The meaning of each field of the expression
Field | Range value | Special characters allowed |
|---|---|---|
second (Seconds) | 0~59 The integer of | , - * / Four characters |
branch (Minutes) | 0~59 The integer of | , - * / Four characters |
Hours (Hours) | 0~23 The integer of | , - * / Four characters |
date (DayofMonth) | 1~31 The integer of ( Actual days need to be considered ) | ,- * ? / L W C Eight characters |
month (Month) | 1~12 Or JAN-DEC | , - * / Four characters |
week (DayofWeek) | 1~7 Or SUN-SAT | , - * ? / L C # Eight characters |
year (Year) | 1970~2099 | , - * / Four characters |
The meaning of special characters :
Special characters | significance |
|---|---|
* | Represents any value that matches the field . If in Minutes If this character is used in the field, it means that the event will be triggered every minute . |
? | Only in DayOfMonth and DayOfWeek Use in . It's used to specify “ An ambiguous value ”. |
- | Specify a range . Such as :“10-12” stay Hour Domain means “10 spot 、11 spot 、12 spot ” Triggering event . |
, | Specify several hash values . Such as : stay Minutes Domain use 10,20, said 10 Points and 20 Trigger an event respectively at the time of minute . |
/ | Refers to the step size . Such as :“0/15” stay Seconds It means the... Of every minute 0 second , The first 15 second , The first 30 Second and the 45 second .“5/15” stay Minutes The field represents the... Per hour 5 minute , The first 20 minute , The first 35 Minute and second 50 minute . Symbol “*” stay “/” front ( Such as :*/10) Equivalent to 0 stay “/” front ( Such as :0/10) |
L | Can only appear in DayOfMonth and DayOfWeek Domain , But the meaning is different in the two fields , for example DayOfMonth Field represents the last day of the month . If in DayOfWeek Domain represents 7 perhaps SAT. If in DayOfWeek Fields are preceded by numbers , It means one day in the last week of a month , for example 5L It means that the event is triggered on the last Thursday of a month . |
W | Means effective working day , Can only appear in DayOfMonth Domain . This character is used to specify the most recent working day of the date .“W” The most recent working day specified by the character cannot span months . character “W” Can only be used with a single value , Can't be a number field , Such as :1-15W It's wrong. . |
LW | L and W It can be used jointly in the date field ,LW Represents the working day of the last week of the month . |
# | Used to determine the week ordinal of each month , Can only appear in DayofMonth Domain . For example, in 4#2, The second Wednesday of a month . namely X#Y, Indicates the day of a month Y A few weeks (X-1). |
C | Only in DayOfMonth Domain and DayOfWeek The domain appears . This character depends on a specified “ The calendar ”. That is to say, the value of this expression depends on the related “ The calendar ” Calculated results of , without “ The calendar ” relation , Then it is equivalent to all included “ The calendar ”. Such as : The date field is “5C” Express Association “ The calendar ” On the first day of middle school , Or after the first day of the month 5 God . Zhou Yu is “1C” Express Association “ The calendar ” On the first day of middle school , Or after the first day of the week 1 God , It's the day after Sunday ( Monday ) |
1.3 Examples of common expressions
Now let's look at a few examples , Examine the above corn Understanding of expression rules , The answer is at the end of the article .
(1) 10 10 10 ? * MON-WED
(2) 0 15 15 ? 5L 2022-2024
(3) 0 0/30 9-17 * * ?
2. corn Application of expressions
2.1 Spring Boot The realization of timing function in
@Configuration
@EnableScheduling
public class SaticScheduleTask {
@Scheduled(cron = "0/5 * * * * ?")
// Or directly specify the time interval , for example :5 second
// @Scheduled(fixedRate=5000)
private void configureTasks() {
. . .
}
}- @Configuration Indicates that this is a configuration class , Both Component The effect of .
- @EnableScheduling Indicates that the timing task is turned on .
- @Scheduled(cron = "0/5 * * * * ?") To add a scheduled task —— The event is triggered every five seconds .
2.2 Linux Of crontab command
stay Linux Medium cron The expression is different from the above , More precisely, it is more streamlined .
crontab The order of is cron The expression + command, there cron The expression has only the following five fields :
Minutes Hours DayOfMonth Month DayOfWeek The operator has : * / - ,
Let's take a look at some examples :
1. Every night 21:30 restart smb
30 21 * * * /etc/init.d/smb restart
2. Every Saturday night 11:00 pm restart smb
0 23 * * 6 /etc/init.d/smb restart
3. evening 11 Point to morning 7 Between points , Restart every hour smb
0 23-7/1 * * * /etc/init.d/smb restart
corn Expression example answer
Come and see if your answer is right ~
(1) 10 10 10 ? * MON-WED
answer : It means every morning from Monday to Wednesday 10 when 10 branch 10 Second trigger event .
(2) 0 15 15 ? 5L 2022-2024
answer : Express 2022 Year to 2024 On the last Thursday of each month in 15 when 15 Sub trigger event .
(3) 0 0/30 9-17 * * ?
answer : It means that the event will be triggered every half an hour from 9 to 5 .
边栏推荐
- Interpretation of OCP function of oceanbase Community Edition
- 黑圆圈显示实现
- 芝诺悖论的计算
- 接口隔离原则
- QQ贴吧那种图片一点开,就变了的原理
- A mechanics informed artistic neural network approach in data driven constructive modeling
- Detailed explanation on persistence of 2022redis7.0x
- moderlarts第一次培训
- VS2010 cannot open when linking sql2008 database
- Black circle display implementation
猜你喜欢

Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode

In unity, opencv and QT are used to create a plugin (I)

AHB2Standard_ handshake_ Bridge design

Show your creativity and win the graphics card! Mmpose attitude estimation creative contest shocks

7905 和TL431 负电压稳压电路 - 相对于电源正极的稳压和浮地电路

Implementation of SQL online editor based on Vue + codemirror

Tensorflow --- creation and reading of tfrecord file

9 r remove missing values

Golang learning notes - Basics

dried food! Information diffusion prediction based on sequence hypergraph neural network
随机推荐
输入值“18-20000hz”错误,设置信息不完整,请选择单位
A mechanics informed artistic neural network approach in data driven constructive modeling
AHB2Standard_ handshake_ Bridge design
Web3 product manager guide - Jason
14 r basic exercises
修改本地微信小程序的AppID
黑圆圈显示实现
Show your creativity and win the graphics card! Mmpose attitude estimation creative contest shocks
Apache APISIX 在Amazon Graviton3 上的安装和性能测试
导师转我800块,让我仿真一个电路(电源设计)
Final examination of theory and practice of socialism with Chinese characteristics 1
A brief talk on shutter button
Systematically study the recommendation system from a global perspective to improve competitiveness in actual combat (Chapter 8)
Black circle display implementation
Ora-01089 ora-19809 ora-19815 exceeded the limit for recovering files
Leetcode 1992. Find all farm groups (yes, once)
APB2standard_ handshake_ Bridge design
【四川大学】初试复试考研资料分享
10 R vector operation construction
15 r exercise