Cron expressions

更新时间:
复制 MD 格式

When you run a Cloud Assistant command in the Elastic Compute Service (ECS) console or by calling an API operation, you can specify a cron expression to define a schedule on which to run the command. This topic describes the basic syntax of cron expressions and provides examples on how to use cron expressions.

Basic syntax of cron expressions

A cron expression is a string that represents the schedule for the execution of a job. A cron expression consists of six or seven fields (time units).

Cron expressions are in the following format:

Seconds Minutes Hours Day-of-month Month Day-of-week [Year]
  • <Year>: This field is optional.

  • For a single field:

    • Each field can contain specific values, ranges, increments, or special characters.

    • If a field has multiple values, separate them with a comma (,).

    • Each field supports up to one leading zero. For example, 01 and 02 are allowed in the fields, but 001 and 002 are not.

For information about the value ranges and special characters supported by fields in cron expressions, see the Valid values of fields and Examples sections in this topic.

Valid values of fields

The following table describes the valid value ranges and special characters supported by the fields in cron expressions.

Field

Required

Value range

Special characters

Seconds

Yes

[0, 59]

* , - /

Minutes

Yes

[0, 59]

* , - /

Hours

Yes

[0, 23]

* , - /

Date

Yes

[1, 31]

* , - / ? L W

Month

Yes

[1, 12] or [JAN, DEC]

* , - /

Day-of-week

Yes

[1, 7] or [MON, SUN]. In Cloud Assistant commands, if you use the [1, 7] format, 1 represents Monday and 7 represents Sunday.

Important

The usage and meanings of cron expressions may vary based on the system, framework, or tool. In scenarios other than Cloud Assistant commands, the meanings of 1 and 7 in [1, 7] may vary. 1 may indicate Sunday and 7 may indicate Saturday.

* , - / ? L #

Year

No

[Current year, 2099]

* , - /

Special characters

Each field in cron expressions supports special characters that have specific meanings.

Special character

Meaning

Example

*

Includes all values.

In the Month field, * means every month.

,

A list of enumeration values.

In the Minutes field, 5,20 triggers once at the 5th minute and once at the 20th minute.

-

Specifies ranges.

In the Minutes field, 5-20 triggers every minute from the 5th to the 20th minute.

/

Specifies increments.

In the Minutes field, 0/15 starts at minute 0 and repeats every 15 minutes. 3/20 starts at minute 3 and repeats every 20 minutes.

?

This indicates an unspecified value and is used only for dates and weeks.

If you specify a value in either the Day-of-month or Day-of-week field, set the other field to ? to avoid conflicts.

L

Stands for "last". It represents the last day and is supported only in the Day-of-month and Day-of-week fields.

Important

Do not combine L with lists or ranges. This combination can cause logical errors.

  • In the Day-of-month field, L means the last day of the month. In the Day-of-week field, L means the last day of the week—Sunday (SUN).

  • If you add a value before L, such as 6L in the Day-of-week field, it means the last Saturday of the month.

W

Represents the nearest weekday (Monday to Friday) to a specified day. The W character does not cross month boundaries. The LW combination represents the last weekday of the month.

In the Day-of-month field, 5W triggers on the nearest weekday to the 5th. If the 5th is a Saturday, it triggers on Friday the 4th. If the 5th is a Sunday, it triggers on Monday the 6th. If the 5th falls on a weekday, it triggers on the 5th.

#

Specifies a day of the week in the month.

Important

This character is supported only in the Day-of-week field.

In the Day-of-week field, 4#2 means the second Thursday of the month.

Examples

Scenario

Value

Run a task at a specific time every day.

Run the task at 10:15 every day.

0 15 10 ? * *

Run the task at 12:00 every day.

0 0 12 * * ?

Run a task at specific times every day.

Run the task at 10:00, 14:00, and 16:00 every day.

0 0 10,14,16 * * ?

Run a task at a fixed interval.

Run the task every half hour from 09:00 to 17:00 every day.

0 0/30 9-17 * * ?

Run the task every minute from 14:00 to 14:59 every day.

0 * 14 * * ?

Run the task every minute from 14:00 to 14:05 every day.

0 0-5 14 * * ?

Run the task every 5 minutes from 14:00 to 14:55 every day.

0 0/5 14 * * ?

Run the task every 5 minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day.

0 0/5 14,18 * * ?

Run a task on specific days of the month or week.

Run the task at 12:00 every Wednesday.

0 0 12 ? * WED

Run the task at 10:15 on the 15th day of every month.

0 15 10 15 * ?

Run the task at 10:15 on the last day of every month.

0 15 10 L * ?

Run the task at 10:15 on the last Saturday of every month.

0 15 10 ? * 6L

Run the task at 10:15 on the 3rd Saturday of every month.

0 15 10 ? * 6#3

Run a task on specific days in specific years.

Run the task at 14:10 and 14:44 every Wednesday in March every year.

0 10,44 14 ? 3 WED

Run the task at 10:15 every day in 2022.

0 15 10 ? * * 2022

Run the task at 10:15 every day every year.

0 15 10 ? * * *

Run the task every 5 minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day in 2022.

0 0/5 14,18 * * ? 2022

Run the task at 10:15 on the 3rd Saturday of every month in 2022 and 2023.

0 15 10 ? * 6#3 2022,2023

Run the task every half hour from 9:00 to 17:30 every day in 2022 to 2025.

0 0/30 9-17 * * ? 2022-2025

Run the task at 14:10 and 14:44 every Wednesday in March every 2 years starting from 2022.

0 10,44 14 ? 3 WED 2022/2