Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 587
Alexander Suraphel Avatar asked Dec 11 '15 07:12

Alexander Suraphel


People also ask

What is CRON expression in a cron job?

A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.

What are the different fields in a cron expression?

Cron Expressions are, in most cases, a string of five fields, which make up the building blocks of every Cron expression. Each field represents a specific time unit, so that the different fields represent minutes, hours, days of the month, months, and days of the week respectively:

What is the default format of a cron expression?

Cron Expression Format : A cron expression is simple a string comprised of anywhere between 6 and 7 fields, each field separated by white space. The most common cron expressions consisting of 7 fields, denoting the various denominations of time, is specified below.

What is CRON expression in org quartz?

Cron Expressions. Cron expressions are used to configure instances of CronTrigger, a subclass of org.quartz.Trigger. A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various ...


2 Answers

* 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
like image 61
Mureinik Avatar answered Sep 22 '22 14:09

Mureinik


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.

like image 34
mirosval Avatar answered Sep 25 '22 14:09

mirosval