CRON Expression: The simplest Explanation post thumbnail image

CRON Expression: The simplest Explanation

A CRON expression is a string compound of six different fields with this structure:

{second} {minute} {hour} {day} {month} {day-of-week}

Each field can have numeric values that are meaningful for the field:

  • second Represents the seconds in a minute. You can assign values from 0 to 59.
  • minute Represents the minutes in an hour. You can assign values from 0 to 59.
  • hour Represents the hours in a day. You can assign values from 0 to 23.
  • day Represents the days in a month. You can assign values from 1 to 31.
  • month Represents the months in a year. You can assign values from 1 to 12. You can also use names in English, such as January, or you can use abbreviations of the name in English, such as Jan. Names are case-insensitive.
  • day-of-week Represents the days of the week. You can assign values from 0 to 6, where 0 is Sunday. You can also use names in English, such as Monday, or you can use abbreviations of the name in English, such as Mon. Names are case-insensitive.

All fields need to be present in a CRON expression. If you don’t want to provide a value to a field, you can use the asterisk character *. This means that the expression uses all available values for that field. For example, the CRON expression * * * * * * means that the trigger is executed every second, in every minute, in every hour, in every day, and every month of the year. You can also use some operators with the allowed values in fields:

  • Range of values Use the dash operator (–) for representing all the values available between two limits. For example, the expression 0 10–12 * * * * means that the function is executed at hh:10:00, hh:11:00, and hh:12:00 where hh means every hour. That is, it is executed three times every hour.
  • Set of values Use the comma operator (,) for representing a set of values. For example, the expression 0 0 11,12,13 * * * means that the function will be executed three times a day, every day, once at 11:00:00, a second time at 12:00:00, and finally at 13:00:00.
  • Interval of values Use the forward slash operator (/) for representing an interval of values. The function is executed when the value of the field is divisible by the value that you put on the right side of the operator. For example, the expression */5 * * * * * will execute the function every five seconds.

Leave a Reply

Related Post