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, 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 |
|
|
A list of enumeration values. |
In the |
|
|
Specifies ranges. |
In the |
|
|
Specifies increments. |
In the |
|
|
This indicates an unspecified value and is used only for dates and weeks. |
If you specify a value in either the |
|
|
Stands for "last". It represents the last day and is supported only in the Important
Do not combine |
|
|
|
Represents the nearest weekday (Monday to Friday) to a specified day. The |
In the |
|
|
Specifies a day of the week in the month. Important
This character is supported 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. |
|
|