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> <Day-of-week> <Month> <Year>
<Year>: This field is optional.
Take note of the following items about each field:
Each field can contain specific values, ranges, increments, or special characters.
Separate multiple values in a field with commas (
,
).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 | Supported special character |
Seconds | Yes | [0, 59] | * , - / |
Minutes | Yes | [0, 59] | * , - / |
Hours | Yes | [0, 23] | * , - / |
Day-of-month | Yes | [1, 31] | * , - / ? L W |
Month | Yes | [1, 12] or [JAN, DEC] | * , - / |
Day-of-week | Yes | [1, 7] or [MON, SUN]. For Cloud Assistant commands, 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 |
| Includes additional values. | In the |
| Specifies ranges. | In the |
| Specifies increments. | In the |
| Specifies any arbitrary value and can be used only in the Day-of-month and Day-of-week fields. | If you specify values in one of the |
| Can be used in the Important To prevent logic errors, do not specify a list or range of values when you use the |
|
| Specifies the weekday that is closest to a specific day of the month. The weekday that the | If you specify |
| Specifies a day of the week in the month. Important This character can be used only in the | In the |
Examples
Scenario | Value | |
Run a task at a specific time every day. | Run the task at 10:15 every day. |
|
Run the task at 12:00 every day. |
| |
Run a task at specific times every day. | Run the task at 10:00, 14:00, and 16:00 every day. |
|
Run a task at a fixed interval. | Run the task every half hour from 09:00 to 17:00 every day. |
|
Run the task every minute from 14:00 to 14:59 every day. |
| |
Run the task every minute from 14:00 to 14:05 every day. |
| |
Run the task every 5 minutes from 14:00 to 14:55 every day. |
| |
Run the task every 5 minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day. |
| |
Run a task on specific days of the month or week. | Run the task at 12:00 every Wednesday. |
|
Run the task at 10:15 on the 15th day of every month. |
| |
Run the task at 10:15 on the last day of every month. |
| |
Run the task at 10:15 on the last Saturday of every month. |
| |
Run the task at 10:15 on the 3rd Saturday of every month. |
| |
Run a task on specific days in specific years. | Run the task at 14:10 and 14:44 every Wednesday in March every year. |
|
Run the task at 10:15 every day in 2022. |
| |
Run the task at 10:15 every day every year. |
| |
Run the task every 5 minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day in 2022. |
| |
Run the task at 10:15 on the 3rd Saturday of every month in 2022 and 2023. |
| |
Run the task every half hour from 9:00 to 17:30 every day in 2022 to 2025. |
| |
Run the task at 14:10 and 14:44 every Wednesday in March every 2 years starting from 2022. |
|