What is a CRON expression?
A CRON expression is a string that consists of six fields that represent a set of times.
The order of the six fields generally is: {second} {minute} {hour} {day} {month} {day of the week}.
For example, a CRON expression to create a trigger that executes every five minutes looks like: 0 */5 * * * *
At first, this string may look confusing. We’ll come back and break down these concepts when we have a deeper look at CRON expressions.
To build a CRON expression, you need to have a basic understanding of some of the special characters.
Now we’ll go back to the original CRON expression example. Let’s try to understand it better by breaking it down field by field.
0 */5 * * * *
The first field represents seconds. This field supports the values 0–59. Because the field contains a zero, it selects the first possible value, which is one second.
The second field represents minutes. The value “*/5” contains two special characters. First, the asterisk (*) means “select every value within the field.” Because this field represents minutes, the possible values are 0–59. The second special character is the slash (/), which represents an increment. When you combine these characters together, it means for all values 0–59, select every fifth value. An easier way to say that is simply “every five minutes.”
The remaining four fields represent the hour, day, month, and weekday of the week. An asterisk for these fields means to select every possible value. In this example, we select “every hour of every day of every month.”
When you put all the fields together, the expression is read as “the first second of every fifth minute of every hour, of every day, of every month”.