简体   繁体   English

之间有区别吗? 和 * 在 cron 表达式中? 奇怪的例子

[英]Is there a difference between ? and * in cron expressions? Strange example

I have the following cron expression in my system:我的系统中有以下 cron 表达式:

0 0 0/1 1/1 * ? *

and you know what?你知道吗? I have no idea what it means.我不知道这意味着什么。 The guy who has written it is on his holiday for the next 2 weeks so I have to find out on myself.写这篇文章的人在接下来的两周里都在放假,所以我必须自己了解一下。 The documentation can be found here文档可以在这里找到

According to the documentation we have:根据文档,我们有:

* * * * * * *
| | | | | | | 
| | | | | | +-- Year              (range: 1970-2099)
| | | | | +---- Day of the Week   (range: 1-7 or SUN-SAT)
| | | | +------ Month of the Year (range: 0-11 or JAN-DEC)
| | | +-------- Day of the Month  (range: 1-31)
| | +---------- Hour              (range: 0-23)
| +------------ Minute            (range: 0-59)
+-------------- Second            (range: 0-59)

Ok, let me tell you what I think: I believe that the expression means:好吧,让我告诉你我的想法:我相信这个表达意味着:

start when:
    seconds:        0
    minutes:        0
    hours:          0
    dayOfMonth      1
    monthOfYear     any
    dayOfWeek       any
    year            any

run every:
    1               hour
    1               dayOfWeek
when:
    dayOfWeek same as on first execution

However available cron expression monitors says that it simply means every hour.然而,可用的 cron 表达式监视器说它只是意味着每小时。 As the one who has written that is Senior Java Dev, he must have known any reason for writing such expression instead of:作为高级 Java Dev 的编写者,他一定知道写这样的表达式的任何原因,而不是:

0 0 * * * * *

We use org.springframework.scheduling.quartz.QuartzJobBean .我们使用org.springframework.scheduling.quartz.QuartzJobBean

Short summary简短的摘要

Well, I think that my question is: what is the difference between 0 0 0/1 1/1 * ? *好吧,我认为我的问题是: 0 0 0/1 1/1 * ? *之间有什么区别0 0 0/1 1/1 * ? * 0 0 0/1 1/1 * ? * and 0 0 * * * * * ? 0 0 0/1 1/1 * ? *0 0 * * * * * ?

Edit:编辑:

The documentation can be found here. 文档可以在这里找到。

0/1 means start at hour 0 and repeat each 1 hour 0/1表示从0小时开始,每1小时重复一次
1/1 is start first day of the month and execute each 1 day 1/1从该月的第一天开始,每1天执行一次

So this pattern executes the cron once each hour, starting day one of month and repeating itself every day. 所以这个模式每小时执行一次cron,从月份的第一天开始,每天重复一次。

there is a requirement to use ? 有要求使用? in one of dayOfWeek or dayOfMonth : dayOfWeekdayOfMonth中的一个:
Support for specifying both a day-of-week and a day-of-month value is not complete (you must currently use the ' ? ' character in one of these fields). 指定星期和月日值的支持尚未完成(您必须在其中一个字段中使用' ? '字符)。 – xenteros 7 mins ago - xenteros 7分钟前

Then, 0 0 * * * ? * 然后, 0 0 * * * ? * 0 0 * * * ? * (and not 0 0 * * * * , with ? mandatory as you commented) will be same expression, ignoring seconds and minutes and taking each value of other elements, will execute each hour and everyday. 0 0 * * * ? * (而不是0 0 * * * *?强制性的,因为你注释)将相同的表达,忽略秒和分钟和服用其他元件的每一个值,将执行每一个小时和日常生活。


According your information: 根据您的信息:

0 0 0/1 1/1 * ? *
| |  |   |  | | | 
| |  |   |  | | +-- Year              (range: 1970-2099)
| |  |   |  | +---- Day of the Week   (range: 1-7 or SUN-SAT)
| |  |   |  +------ Month of the Year (range: 0-11 or JAN-DEC)
| |  |   +--------- Day of the Month  (range: 1-31)
| |  +------------- Hour              (range: 0-23)
| +---------------- Minute            (range: 0-59)
+------------------ Second            (range: 0-59)

And this explanation of the special characters: 这个特殊字符的解释

* (“all values”) * (“所有价值”)

used to select all values within a field. 用于选择字段中的所有值。 For example, “” in the minute field means *“every minute”. 例如,分钟字段中的“”表示“每分钟”。

? (“no specific value”) (“没有特定价值”)

useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. 当您需要在允许该字符的两个字段之一中指定某些内容时有用,而不是另一个字段。 For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put “10” in the day-of-month field, and “?” in the day-of-week field. 例如,如果我希望我的触发器在该月的某个特定日期(例如,第10天)触发,但不关心恰好在一周的哪一天,我会在当天放置“10” -month字段,以及星期几字段中的“?”。

/

used to specify increments. 用于指定增量。 For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. 例如,秒字段中的“0/15”表示“秒0,15,30和45”。 And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. 并且秒字段中的“5/15”表示“秒5,20,35和50”。 You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. 你也可以在''字符后指定'/' - 在这种情况下''相当于在'/'之前有'0'。 '1/3' in the day-of-month field means “fire every 3 days starting on the first day of the month”. 日期字段中的“1/3”表示“从该月的第一天开始每3天触发一次”。


differences between * and ? *?之间的差异

To explain difference between ? 解释之间的区别? and * in the expressions, first of all take a look at this table: *在表达式中,首先看一下这个表:

Field Name      Mandatory   Allowed Values      Allowed Special Characters
Seconds         YES         0-59                , - * /
Minutes         YES         0-59                , - * /
Hours           YES         0-23                , - * /
Day of month    YES         1-31                , - * ? / L W   //allowed '?'
Month           YES         1-12 or JAN-DEC     , - * /
Day of week     YES         1-7 or SUN-SAT      , - * ? / L #   //allowed '?'
Year            NO          empty, 1970-2099    , - * /

As you can see ? 如你所见? is only allowed in Day of month and Day of week is mandatory in one of both fields and will tell Quartz this value has not been defined, thus, use the other field (if you put ? into Day of month , the value used will be Day of week ). 只允许在Day of month并且在两个字段之一中必须使用Day of week ,并告诉Quartz此值尚未定义,因此,使用另一个字段(如果您将?放入Day of month ,则使用的值将是Day of week )。

There is no practical difference between 0 0 * * * ? * 0 0 * * * ? *之间没有实际区别0 0 * * * ? * 0 0 * * * ? * and 0 0 0/1 1/1 * ? * 0 0 * * * ? *0 0 0/1 1/1 * ? * 0 0 0/1 1/1 * ? *

Analyzing different marks: 分析不同的标记:
0/1 and * for hours - first means start from hour 0 every day and repeat every hour, second one means: repeat every hour 0/1*表示小时 - 首先表示每天从小时0开始,每小时重复一次,第二表示:每小时重复一次
1/1 and * for days - fisrt means start from first day of the month and repeats every day and the second one means every day. 1/1*天 - fisrt表示从月的第一天开始,每天重复,第二天表示每天。

Reason why someone used complex expression maybe is that by testing, expression evaluated to this form and nobody undertook the job to simplify it or maybe previous cron version worked different. 有人使用复杂表达式的原因可能是通过测试,表达式评估为这种形式,并且没有人承担简化它的工作或者可能以前的cron版本工作不同。

Not an answer, just an update to the correct answer of @joc. 不是答案,只是更新@joc的正确答案。

As for now, QuartzScheduler specifically points it out that ? 至于现在, QuartzScheduler特别指出了这一点? could be applied in one of the two positions: day_of_month or day_of_week . 可以应用于以下两个位置之一: day_of_monthday_of_week

Support for specifying both a day-of-week and a day-of-month value is not complete (you must currently use the '?' character in one of these fields). 指定星期和月日值的支持尚未完成(您必须在其中一个字段中使用'?'字符)。

Besides in the link enclosed above, there are lots of examples to guide you well enough to come up with your own. 除了上面的链接之外,还有很多例子可以指导您提供自己的链接。


+--------------------+-------------------------------------------------------------------------------------------------------------------------------------+
|   **Expression**   |                                                             **Meaning**                                                             |
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| 0 0 12 * * ?       | Fire at 12pm (noon) every day                                                                                                       |
| 0 15 10 ? * *      | Fire at 10:15am every day                                                                                                           |
| 0 15 10 * * ?      | Fire at 10:15am every day                                                                                                           |
| 0 15 10 * * ? *    | Fire at 10:15am every day                                                                                                           |
| 0 15 10 * * ? 2005 | Fire at 10:15am every day during the year 2005                                                                                      |
| 0 * 14 * * ?       | Fire every minute starting at 2pm and ending at 2:59pm, every day                                                                   |
| 0 0/5 14 * * ?     | Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day                                                                |
| 0 0/5 14,18 * * ?  | Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day |
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------+

You can use this website as a quick way to generate and understand cron expressions syntax without much knowledge about the cron format syntax: http://www.cronmaker.com您可以使用此网站作为快速生成和理解 cron 表达式语法的方法,而无需对 cron 格式语法有太多了解: http : //www.cronmaker.com

The website uses Quartz cron format.该网站使用 Quartz cron 格式。 For example, your expression means the next scheduled dates are in every hour:例如,您的表达式表示下一个预定日期是每小时: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM