Cron Expression: What exactly is the difference between ? and * in a cron expression?
Asked Answered
C

4

13

It appears to me that both mean "any of the available values". What exactly in the difference between them?

Cecilacecile answered 11/12, 2015 at 7:34 Comment(0)
Z
18

* means every possible value in the field. ? means you don't care about the value. It's used when you have two fields that may contradict each other. The common example being the day of month and day of week fields. Consider, for example a cron specification for running at 10AM on the first day of every month:

0 0 10 1 * ? *

Now let's break it down:

  • Seconds: 0 - we want it to run on 10:00:00
  • Minutes: 0 - we want it to run on 10:00:00
  • Hours: 10 - we want it to run on 10:00:00
  • Day of month: 1 - we want it to run of the 1st of every month
  • Month: * - we want it to run on every month (e.g., January 1st, February 1st, etc.)
  • Day of week: ? - we don't care about the day of week. The cron should run on the 1st of every month, regardless of whether it's a Sunday, a Monday, etc.
  • Year: * - we want it to run on every year
Zootoxin answered 11/12, 2015 at 7:44 Comment(1)
So you're saying, we can't specify 1 in "day-of-month" and * in "day-of-week" because we can't specify a specific value (or "all values" = *) for both fields. So ? is just a way of consuming the "day-of-week" field without conflicting with "day-of-month" so we can specify a value for "year" after it? (Asking because another question said it wasn't clear.)Epistaxis
J
6

From Quartz Scheduler

* ("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. See the examples below for clarification.

Jenniejennifer answered 11/12, 2015 at 7:41 Comment(0)
C
1

The * character is used to specify all values. For example, "*" in the minute field means " every minute ".

The ? character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fields, but not the other. See the examples below for clarification.

You can look more here: http://docs.netkernel.org/book/view/book:mod:cron/doc:mod:cron:cronexpression

Also if you need to create a Cron expression you can use this: http://www.cronmaker.com/

Casiano answered 11/12, 2015 at 7:42 Comment(0)
L
0
  • * means every time. According to the position, it will be: every second, minute, hour, day, month.
  • ? means that it would be chosen by other field. So ? doesn't add filter, but it wouldn't run every time.

? is used for setting the day, as it might be a day of month or the day of week. So if you choose the day of month (like the 1-st day of month), than you put ? in the day of week, by which you show, that it would be not every day of week, but only those days which are allowed by other conditions. And vice versa, if you set day of week, you put ? in the day of month.

Lareelareena answered 5/2, 2023 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.