Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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. As the one who has written that is Senior Java Dev, he must have known any reason for writing such expression instead of:

0 0 * * * * * 

We use 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 * ? * and 0 0 * * * * *?

Edit:

The documentation can be found here.

like image 929
xenteros Avatar asked Sep 05 '16 10:09

xenteros


People also ask

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

What is cron expression 0 * * * *?

Meaning of cron expression 0 * * * * *? I think it means the scheduler is expected to run every seconds.

What does asterisk mean in cron?

Crontab Operators The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'..


1 Answers

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

like image 133
joc Avatar answered Sep 23 '22 15:09

joc