All Products
Search
Document Center

Elastic Compute Service:Cron expressions

Last Updated:Jan 30, 2024

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, 1 in [1, 7] indicates Monday and 7 indicates 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, the asterisk (*) includes every month.

,

Includes additional values.

In the Minutes field, 5,20 denotes the 5th and 20th minutes of the hour.

-

Specifies ranges.

In the Minutes field, 5-20 denotes the 5th minute to the 20th minute of the hour.

/

Specifies increments.

In the Minutes field, 0/15 denotes every 15th minute, starting on the hour. In the Minutes field, 3/20 denotes every 20th minute, starting from the 3rd minute of the hour.

?

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 Day-of-month and Day-of-week fields, enter a question mark (?) in the other field to avoid date conflicts.

L

Can be used in the Day-of-month or Day-of-week field to specify the last day of the month or week.

Important

To prevent logic errors, do not specify a list or range of values when you use the L character.

  • In the Day-of-month field, L denotes the last day of the month. In the Day-of-week field, L denotes the last day of the week, which is Sunday (SUN).

  • L can be used with other values. For example, in the Day-of-week field, 6L denotes the last Saturday of the month.

W

Specifies the weekday that is closest to a specific day of the month. The weekday that the W character denotes is in the same month as the specified day of the month. LW denotes the last weekday of the specified month.

If you specify 5W in the Day-of-month field and the 5th day of the month falls on a Saturday, a task is triggered on the nearest Friday, which is the 4th day of the month. If the 5th day of the month falls on a Sunday, a task is triggered on the nearest Monday, which is the 6th day of the month. If the 5th day of the month falls on a weekday, a task is triggered on the 5th day of the month.

#

Specifies a day of the week in the month.

Important

This character can be used only in the Day-of-week field.

In the Day-of-week field, 4#2 denotes the 2nd 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