AnalyticDB for PostgreSQL uses cron expressions to schedule periodic plans. This topic describes cron expressions and their examples.
A cron expression is a string that describes the details of a schedule. Each string
consists of six or seven fields separated by spaces. The fields contain the schedule
details in the following order: Second Minute Hour Day Month Week Year
. The Year field is optional. When a field contains more than one value, each value
is separated by a comma (,). Each field can contain definite values or wildcards.
Field values
The following table describes the allowed values and wildcard characters for cron expression fields.
Field | Required | Valid value range | Wildcard |
---|---|---|---|
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 MON-SUN. If you use the numerical representation, 1 indicates Monday and 7 indicates Sunday.
|
* , - / ? L # |
Year | No | [1970, 2099] | * , - / |
Wildcards
All fields in a cron expression support wildcards. Each wildcard has a specific meaning.
Wildcard | Description | Example |
---|---|---|
* |
Indicates all values. | In the Month field, an asterisk (* ) indicates every month. In the Week field, an asterisk (* ) indicates every day of a week.
|
, |
Lists enumerated values. | In the Minute field, 5,20 indicates that a plan is triggered once at both the 5th and 20th minutes.
|
- |
Indicates a range. | In the Minute field, 5-20 indicates that a plan is triggered once every minute from the 5th to 20th minute.
|
/ |
Indicates increments. | In the Minute field, 0/15 indicates that a plan is triggered once every 15 minutes from the beginning of an
hour. 3/20 indicates that a plan is triggered once every 20 minutes from the beginning of the
3rd minute.
|
? |
Indicates that no value is specified. This character is supported only for the Day and Week fields. | If the Day or Week field is specified, the other field must be set to a question mark
(? ) to prevent conflicts.
|
L |
Indicates the last day. This character is supported only for the Day and Week fields.
Note To prevent logic errors, do not specify a list or a range when you use the
L character.
|
|
W |
Indicates the business day that is nearest to the specified day of the month. The
business day that the W character specifies is in the same month as the specified day of the month. LW indicates the last business day of the specified month. This character is supported
only for the Day field.
|
If 5W is specified in the Day field and the fifth day of the month is a Saturday, a plan
is triggered on the nearest business day, which is Friday, the fourth day of the month.
If the fifth day of the month is a Sunday, a plan is triggered on the nearest business
day, which is Monday, the sixth day of the month. If the fifth day of the month is
from Monday to Friday, a plan is triggered on the fifth day.
|
# |
Indicates a specific day of a specific week in every month. This character is supported only for the Week field. | In the Week field, 4#2 indicates the second Thursday of a month.
|
Examples
The following table describes some example values of cron expressions.
Scenario | Example (UTC) | Example (UTC+8) |
---|---|---|
Executes a plan at 10:15 every day. | 0 15 10 ? * * |
0 15 18 ? * * |
Executes a plan at 10:15 every day. | 0 15 10 * * ? |
0 15 18 * * ? |
Executes a plan at 12:00 every day. | 0 0 12 * * ? |
0 0 20 * * ? |
Executes a plan at 10:00, 14:00, and 16:00 every day. | 0 0 10,14,16 * * ? |
0 0 18,22,0 * * ? |
Executes a plan every half an hour from 09:00 to 17:00 every day. | 0 0/30 9-17 * * ? |
0 0/30 17-2 * * ? |
Executes a plan every minute from 14:00 to 14:59 every day. | 0 * 14 * * ? |
0 * 22 * * ? |
Executes a plan every minute from 14:00 to 14:05 every day. | 0 0-5 14 * * ? |
0 0-5 22 * * ? |
Executes a plan every 5 minutes from 14:00 to 14:55 every day. | 0 0/5 14 * * ? |
0 0/5 22 * * ? |
Executes a plan every 5 minutes from 14:00 to 14:55 and from 18:00 to 18:55 every day. | 0 0/5 14,18 * * ? |
0 0/5 22,3 * * ? |
Executes a plan at 12:00 every Wednesday. | 0 0 12 ? * WED |
0 0 20 ? * WED |
Executes a plan at 10:15 on the 15th day of every month. | 0 15 10 15 * ? |
0 15 18 15 * ? |
Executes a plan at 10:15 on the last day of every month. | 0 15 10 L * ? |
0 15 18 L * ? |
Executes a plan at 10:15 on the last Saturday of every month. | 0 15 10 ? * 6L |
0 15 18 ? * 6L |
Executes a plan at 10:15 on the third Saturday of every month. | 0 15 10 ? * 6#3 |
0 15 18 ? * 6#3 |
Executes a plan at 14:10 and 14:44 every Wednesday in March every year. | 0 10,44 14 ? 3 WED |
0 10,44 22 ? 3 WED |
Executes a plan at 10:15 every day in 2022. | 0 15 10 ? * * 2022 |
0 15 18 ? * * 2022 |
Executes a plan at 10:15 every day every year. | 0 15 10 ? * * * |
0 15 18 ? * * * |
Executes a plan 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 |
0 0/5 22,3 * * ? 2022 |
Executes a plan at 10:15 on the third Saturday of every month from 2022 to 2023. | 0 15 10 ? * 6#3 2022,2023 |
0 15 18 ? * 6#3 2022,2023 |
Executes a plan every half an hour from 9:00 to 17:30 every day from 2022 to 2025. | 0 0/30 9-17 * * ? 2022-2025 |
0 0/30 17-2 * * ? 2022-2025 |
Executes a plan at 14:10 and 14:44 every Wednesday in March every two years starting from 2022. | 0 10,44 14 ? 3 WED 2022/2 |
0 10,44 22 ? 3 WED 2022/2 |