42

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.

3
  • how much time ago was this expression written? maybe there were limitations in special characters allowed in quartz when it was created
    – joc
    Commented Sep 5, 2016 at 10:27
  • 1
    @joc long ago, and it seems, there was a requirement to use ? in one of dayOfWeek and 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
    Commented Sep 5, 2016 at 10:32
  • 1
    nice catch!! I didn't know that, but as you stated in the question 0 0 * * ? * is an equivalent expression to the 0 0 0/1 1/1 * ? * IMO at least nowadays
    – joc
    Commented Sep 5, 2016 at 10:37

4 Answers 4

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

3
  • 1
    Ok, your answer is useful, however I won't accept it yet, as you haven't answered directly my question. What is the advantage of using ? over *? I'll accept this answer in a day or two if there is no other answer which would point out the difference.
    – xenteros
    Commented Sep 5, 2016 at 10:27
  • 5
    well, actually your question is what is the difference between 0 0 0/1 1/1 * ? * and 0 0 * * * * *? but the difference is ? means NO SPECIFIC VALUE, * means ALL VALUES
    – joc
    Commented Sep 5, 2016 at 10:29
  • 1
    I started a bounty on this question. Feel free to improve your answer. At the moment it's +50 repu for the best answer in a week, but if I'm able to award more.
    – xenteros
    Commented Oct 10, 2016 at 7:36
7

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.

1

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 |
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------+

1

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

The website uses Quartz cron format. For example, your expression means the next scheduled dates are in every hour: enter image description here

1
  • An error occurred ! Main page
    – Nakilon
    Commented Jan 7 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.