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

后端 未结 3 1927
误落风尘
误落风尘 2020-12-14 07:55

I have the following cron expression in my system:

0 0 0/1 1/1 * ? *

and you know what? I have no idea what it means. The guy who has writt

相关标签:
3条回答
  • 2020-12-14 08:40

    Not an answer, just an update to the correct answer of @joc.

    As for now, QuartzScheduler specifically points it out that ? could be applied in one of the two positions: day_of_month or day_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 |
    +--------------------+-------------------------------------------------------------------------------------------------------------------------------------+
    
    
    0 讨论(0)
  • 2020-12-14 08:43

    There is no practical difference between 0 0 * * * ? * and 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
    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.

    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.

    0 讨论(0)
  • 2020-12-14 08:50

    0/1 means start at hour 0 and repeat each 1 hour
    1/1 is start first day of the month and execute each 1 day

    So this pattern executes the cron once each hour, starting day one of month and repeating itself every day.

    there is a requirement to use ? in one of dayOfWeek or dayOfMonth:
    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

    Then, 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.


    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.

    /

    used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. You can also specify ‘/’ after the ‘’ character - in this case ‘’ is equivalent to having ‘0’ before the ‘/’. ‘1/3’ in the day-of-month field means “fire every 3 days starting on the first day of the month”.


    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).

    0 讨论(0)
提交回复
热议问题