Use cron expression [fly] more comprehensive use of cron expression

 

turn:

Cron expression usage

cron expression by specific rules specified time for regular tasks, simply record some of its syntax and examples, not entirely, to cover most of their daily needs.

 

1. The overall structure

cron expression is a string, divided into six or seven fields, each separated by a space between two domains, which syntax is:

'S domain and domain-day time-domain domain domain week of May domain domain "

Among them, the domain can be omitted, represent a year when omitted.

 

2. Each domain range

domain name Possible values Preferably symbols (column only partially used)
The seconds field An integer of 0 to 59   *    -    ,    /
Fenwick An integer of 0 to 59   *    -    ,    /
Time Domain An integer of 0 to 23   *    -    ,    /
Day range An integer of 1 to 31,   *    -    ,    /    ?    L
Domain month Or an integer of 1 to 12 JAN ~ DEC   *    -    ,    /
Zhou domain Or an integer of 1 to 7 SUN ~ SAT   *    -    ,    /    ?    L    # 
In the domain It is an integer of 1970 to 2099   *    -    ,    /

 

3. The significance of symbols

(1) * can represent all the values ​​that match the domain;

For example, "0013 * *?" Indicates that the implementation of a daily 13 points.

(2)? Must appear in cron expressions and can only occur once, on the day with a domain or domain week, represents uncertain value, not the limit value;

It differs from * that does not match all values ​​on that domain, such as specifying a March every Friday, so Friday March 4 may be value;

For example, "0056 *?" Represents the 6th of each month at 5 o'clock executed regardless of the week, "005? * 3" every Wednesday at 5 o'clock and perform a few numbers either.

(3) - represents a range in the match field;

For example, "? 0309-15 * *" indicates that the daily 9: 30-15: 30 is performed once per hour, "01-5 * * 1?" Indicates that every Sunday 1-5 minutes per each hour minutes once.

(4) shows enumeration value;

For example, "00 8,16,23? * 1,7" indicates that each of Saturday and Sunday 8:00, 16:00, 23:00 time of each execution.

(5) / represents the start time and time interval;

For example, "00 * 3/5 *?" Indicates 3 o'clock every day started, is performed once every five hours, i.e. 3:00, 8:00, 13:00, 18:00, 23:00 is executed once,

"* / 5 * * * *?" Means every 5 seconds to perform a "0 * / 1 * * *?" Means every minute once.

(6) it can only be used on the periphery of domain #, # behind the digital representation of a few weeks, if the value of the week does not exist, is not performed;

For example, "000? * 5 # 2" indicates the second Thursday of each month the implementation of the zero point, "15,306? * 7 # 5" indicates the fifth Saturday of each month to perform 6:30:15 If a month does not exist the fifth Saturday, is not performed.

Represents the last day of the month, if used without the first circumferential region numerals Saturday, indicated by numbers in the last cycle value (7) L domain only for days or weeks field for the date field;

For example, "0 0 23 L *?" Represents the last day of each month 23:00 executed, "0 0 23? * L" represents every Saturday 23:00 executed, "0 0 23? * 3L" represents the last month Tuesday's 23 points execution.

 

4. Some common examples (not double quotes)

significance expression
 Executed once every 5 seconds            */5  *  *  *  *  ?
 Executed once every minute            0  */1  *  *  *  ?
 1:00 executed once a day     0  0  1  *  *  ?
 23:55 executed once a day     0  55  23  *  *  ?
 The last day of each month 23:00 executed once     0  0  23  L  *  ?
 Saturday 8:00 execution time     0  0  8  ?  *  L
 The last Friday of every month, performed once every 2 hours     0  0  */2  ?  *  6L
 Third Friday of each month at 10:15 am execution time     0  15  10  ?  *  5#3
 Execute every 1 minute during the day at 2:00 pm to 2:05 pm     0  0-5  14  *  *  ?
 She said on Monday through Friday at 10:15 am every day to perform     0  15  10  ?  *  2-6
 每个月的最后一个星期五上午10:15执行     0  15  10  ?  *  6L 
 每天上午10点,下午2点,4点执行一次     0  0  10,14,16  * * ?
 朝九晚五工作时间内每半小时执行一次     0  0/30  9-17  *  * ?
 每个星期三中午12点执行一次     0  0  12  ?  *  4
 每年三月的星期三的下午2:10和2:44各执行一次       0  10,44  14  ?  3  4 
 每月的第三个星期五上午10:15执行一次     0  15  10  ?  *  6#3
 每月一日凌晨2点30执行一次     0  30  2  1  *  ?
 每分钟的第10秒与第20秒都会执行     10,20  *  *  *  * ?
 每月的第2个星期的周5,凌晨执行     0  0  0  ?  *  6#2

 

cron表达式通过特定的规则指定时间,用于定时任务,本文简单记录它的部分语法和实例,并不完全,能覆盖日常大部分需求。

 

1. 整体结构

cron表达式是一个字符串,分为6或7个域,每两个域之间用空格分隔,其语法格式为:

"秒域 分域 时域 日域 月域 周域 年域"

其中,年域可以省略,省略时表示每年。

 

2. 每个域的取值范围

域名 可取值 可取符号(仅列部分常用)
秒域 0~59的整数   *    -    ,    /
分域 0~59的整数   *    -    ,    /
时域 0~23的整数   *    -    ,    /
日域 1~31的整数   *    -    ,    /    ?    L
月域 1~12的整数或JAN~DEC   *    -    ,    /
周域 1~7的整数或SUN~SAT   *    -    ,    /    ?    L    # 
年域 1970~2099的整数   *    -    ,    /

 

3. 符号的意义

(1)* 表示可以匹配该域的所有值;

例如,"0 0 13 * * ?"表示每天的13点执行一次。

(2)?在cron表达式中必须出现且只能出现1次,用在日域或周域上,表示不确定值、不限制值;

它与*的区别是,并不能匹配到该域上的所有值,例如指定了3月份每个周五,那么3月份的周五可能只有4个值;

例如,"0 0 5 6 * ?"表示每个月6日5点钟执行而无论周几,"0 0 5 ? * 3"表示每个周三5点钟执行而无论是几号。

(3)- 表示匹配该域上的一个范围;

例如,"0 30 9-15 ? * *"表示每天的9:30-15:30每小时执行一次,"0 1-5 * ? * 1"表示每周日每个小时的1-5分钟每分钟执行一次。

(4), 表示枚举值;

例如,"0 0 8,16,23 ? * 1,7"表示每个周六日的8点、16点、23点各执行一次。

(5)/ 表示起始时间和间隔时间;

例如,"0 0 3/5 * * ?"表示每天3点开始执行,每隔5个小时执行一次,即3点、8点、13点、18点、23点各执行一次,

"*/5 * * * * ?"表示每隔5秒钟执行一次,"0 */1 * * * ?"表示每隔1分钟执行一次。

(6)# 只能用于周域上,#后面的数字表示第几周,如果不存在这个周的值,则不执行;

例如,"0 0 0 ? * 5#2"表示每个月第2个周四零点执行,"15 30 6 ? * 7#5"表示每个月第5个周六6:30:15执行,如果某个月不存在第5个周六,则不执行。

(7)L 只能用于日域或周域,用于日域时表示当月最后一天,用于周域如果前面不加数字表示周六,加数字表示最后一个周值;

例如,"0 0 23 L * ?"表示每月最后一天的23点执行,"0 0 23 ? * L"表示每周六的23点执行,"0 0 23 ? * 3L"表示每月最后一个周二的23点执行。

 

4. 一些常见的实例(省略双引号)

意义 表达式
 每隔5秒钟执行一次            */5  *  *  *  *  ?
 每隔1分钟执行一次            0  */1  *  *  *  ?
 每天1点执行一次     0  0  1  *  *  ?
 每天23点55分执行一次     0  55  23  *  *  ?
 每月最后一天23点执行一次     0  0  23  L  *  ?
 每周六8点执行一次     0  0  8  ?  *  L
 每月最后一个周五,每隔2小时执行一次     0  0  */2  ?  *  6L
 每月的第三个星期五上午10:15执行一次     0  15  10  ?  *  5#3
 在每天下午2点到下午2:05期间的每1分钟执行     0  0-5  14  *  *  ?
 表示周一到周五每天上午10:15执行     0  15  10  ?  *  2-6
 每个月的最后一个星期五上午10:15执行     0  15  10  ?  *  6L 
 每天上午10点,下午2点,4点执行一次     0  0  10,14,16  * * ?
 朝九晚五工作时间内每半小时执行一次     0  0/30  9-17  *  * ?
 每个星期三中午12点执行一次     0  0  12  ?  *  4
 每年三月的星期三的下午2:10和2:44各执行一次       0  10,44  14  ?  3  4 
 每月的第三个星期五上午10:15执行一次     0  15  10  ?  *  6#3
 每月一日凌晨2点30执行一次     0  30  2  1  *  ?
 每分钟的第10秒与第20秒都会执行     10,20  *  *  *  * ?
 每月的第2个星期的周5,凌晨执行     0  0  0  ?  *  6#2

 

Guess you like

Origin www.cnblogs.com/libin6505/p/11277939.html