当前位置:网站首页>[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

theme: smartblue

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() {
        . . .
    }
}
  1. @Configuration Indicates that this is a configuration class , Both Component The effect of .
  2. @EnableScheduling Indicates that the timing task is turned on .
  3. @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 .

原网站

版权声明
本文为[Yijun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011746472490.html