edit-icon download-icon

Cron expressions

Last Updated: Mar 07, 2018

On ECS instances, you can set time-based scheduled programs by using Cron commands. You can use Cron in a scheduled maintenance feature, for example, cloud assistant. By setting parameters with Cron expressions to complete periodical tasks, the instance auto runs regular daily tasks or individual tasks at a specified time.

Cron expression description

The value of Cron expression is a string with time implications. The string is separated by five or six spaces into six or seven fields, in the format of X X X X X X or X X X X X X X. Specifically, X is a placeholder of a field. When a field contains multiple values, you can use commas (,) to separate them. Each field can be a determined value, or a special character with logical meanings. The following table lists values and supported special characters for the seven fields in Cron expressions.

Field Required Value range Special character
Second Yes [0, 59] * , - /
Minute Yes [0, 59] * , - /
Hour Yes [0, 23] * , - /
Day Yes [1, 31] * , - / ? L W
Month Yes [1, 12] or [JAN, DEC] * , - /
Week Yes [1, 7] or [SUN, SAT]
If you are using [1, 7] expressions, 1 stands for Sunday, and 2 stands for Monday.
* , - / ? L #
Year No [1970, 2099] * , - /

Meanings of special characters

As shown in the preceding table, each field in Cron expressions supports a certain number of special characters and each special character has its corresponding meanings.

  • *: It indicates all possible values.

    Example:

    • In the "Month" field, * indicates any month.
    • In the "Week" field, * indicates any day of the week.
  • ,: It is used to list enumeration values.

    Example:
    In the "Minute" field, 5,20 indicate to trigger the command process once at the 5th and 20th minute respectively.

  • -: It defines a scope.

    Example:
    In the "Minute" field, 5-20 indicates to trigger the command process once every other minute from the 5th to the 20th minute.

  • /: It indicates the increment of the specified value.

    Example:

    • In the "Minute" field, 0/15 indicates to trigger the command process once every 15 minutes from the beginning.
    • In the "Minute" field, 3/20 indicates to trigger the command process once every 20 minutes from the 3rd minute.
  • ?: It indicates an unspecified value. Only the "Date" and "Week" fields support this character. When the "Date" or the "Week" field is specified with values, the value of the other field must be set to ? to avoid conflicts.

  • L: It indicates "last". Only the "Date" and "Week" fields support this character. The meanings of L in the two fields are different. If L is preceded by specific content, such as 6L in the "Week" field, it indicates the last Friday of the month.

    • In the "Date" field, L indicates the last day of the month.
    • In the "Week" field, L indicates Saturday (SAT).

      Note:
      Avoid specifying a list or scope when using the L character. Otherwise it may lead to logical disorder.

  • W: It indicates valid workdays other than weekends. The system will trigger the event on the valid workday nearest to the specified date. The workday that the W character finalizes on will only be in the same month. LW indicates the last workday of the specified month.

    Example:
    When 5W is used in the "Date" field, if the 5th day of the month falls on Saturday, the field considers the upcoming Friday as the 4th day. If the 5th day of the month falls on Sunday, the field considers the nearest Monday as the 6th day of the month. If the 5th day of the month falls between Monday and Friday, the event is triggered on the 5th.

  • #: It indicates a specific day of the week in every month. Only the "Week" field supports this character.

    Example:
    In the "Week" field, 4#2 indicates the 2nd Wednesday of a month.

Examples of Cron expression

Examples of Cron expression values are listed as follows.

Example Description
0 15 10 ? * * Perform the task at 10:15 every day
0 15 10 * * ? Perform the task at 10:15 every day
0 15 10 * * ? * Perform the task at 10:15 every day
0 0 12 * * ? Perform the task at 12:00 every day
0 0 10,14,16 * * ? Perform the task at 10:00, 14:00 and 16:00 every day
0 0/30 9-17 * * ? Perform the task once every half an hour from 09:00 to 17:00 every day
0 * 14 * * ? Perform the task once every other minute from 14:00 to 14:59 every day
0 0-5 14 * * ? Perform the task once every other minute from 14:00 to 14:05 every day
0 0/5 14 * * ? Perform the task once every five minutes from 14:00 to 14:55 every day
0 0/5 14,18 * * ? Perform the task once every five minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day
0 0 12 ? * WED Perform the task at 12:00 every Wednesday
0 15 10 ? * MON-FRI Perform the task at 10:15 from Monday to Friday every week
0 0 2 1 * ? * Perform the task at 02:00 on the 1st day of every month
0 15 10 15 * ? Perform the task at 10:15 on the 15th day of every month
0 15 10 L * ? Perform the task at 10:15 on the last day of every month
0 15 10 ? * 6L Perform the task at 10:15 on the last Friday of every month
0 15 10 ? * 6#3 Perform the task at 10:15 on the third Friday of every month
0 10,44 14 ? 3 WED Perform the task from 14:10 to 14:44 on every Wednesday in every March
0 15 10 ? * 6L 2002-2005 Perform the task at 10:15 on the last Friday of every month from 2002 to 2005
0 15 10 * * ? 2005 Perform the task at 10:15 every day in 2005
0 15 10 ? * 6L 2002-2006 Perform the task at 10:15 on the last Friday of every month from 2002 to 2006

References

You can use Cron expressions for the following:

Thank you! We've received your feedback.