|
| 1 | +[[cron-expressions]] |
| 2 | +=== Cron expressions |
| 3 | + |
| 4 | +A cron expression is a string of the following form: |
| 5 | + |
| 6 | +[source,txt] |
| 7 | +------------------------------ |
| 8 | + <seconds> <minutes> <hours> <day_of_month> <month> <day_of_week> [year] |
| 9 | +------------------------------ |
| 10 | + |
| 11 | +{es} uses the cron parser from the http://www.quartz-scheduler.org[Quartz Job Scheduler]. |
| 12 | +For more information about writing Quartz cron expressions, see the |
| 13 | +http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-06.html[Quartz CronTrigger Tutorial]. |
| 14 | + |
| 15 | +All schedule times are in coordinated universal time (UTC); other timezones are not supported. |
| 16 | + |
| 17 | +TIP: You can use the <<elasticsearch-croneval>> command line tool to validate your cron expressions. |
| 18 | + |
| 19 | + |
| 20 | +[[cron-elements]] |
| 21 | +==== Cron expression elements |
| 22 | + |
| 23 | +All elements are required except for `year`. |
| 24 | +See <<cron-special-characters>> for information about the allowed special characters. |
| 25 | + |
| 26 | +`<seconds>`:: |
| 27 | +(Required) |
| 28 | +Valid values: `0`-`59` and the special characters `,` `-` `*` `/` |
| 29 | + |
| 30 | +`<minutes>`:: |
| 31 | +(Required) |
| 32 | +Valid values: `0`-`59` and the special characters `,` `-` `*` `/` |
| 33 | + |
| 34 | +`<hours>`:: |
| 35 | +(Required) |
| 36 | +Valid values: `0`-`23` and the special characters `,` `-` `*` `/` |
| 37 | + |
| 38 | +`<day_of_month>`:: |
| 39 | +(Required) |
| 40 | +Valid values: `1`-`31` and the special characters `,` `-` `*` `/` `?` `L` `W` |
| 41 | + |
| 42 | +`<month>`:: |
| 43 | +(Required) |
| 44 | +Valid values: `1`-`12`, `JAN`-`DEC`, `jan`-`dec`, and the special characters `,` `-` `*` `/` |
| 45 | + |
| 46 | +`<day_of_week>`:: |
| 47 | +(Required) |
| 48 | +Valid values: `1`-`7`, `SUN`-`SAT`, `sun`-`sat`, and the special characters `,` `-` `*` `/` `?` `L` `#` |
| 49 | + |
| 50 | +`<year>`:: |
| 51 | +(Optional) |
| 52 | +Valid values: `1970`-`2099` and the special characters `,` `-` `*` `/` |
| 53 | + |
| 54 | +[[cron-special-characters]] |
| 55 | +==== Cron special characters |
| 56 | + |
| 57 | +`*`:: |
| 58 | +Selects every possible value for a field. For |
| 59 | +example, `*` in the `hours` field means "every hour". |
| 60 | + |
| 61 | +`?`:: |
| 62 | +No specific value. Use when you don't care what the value |
| 63 | +is. For example, if you want the schedule to trigger on a |
| 64 | +particular day of the month, but don't care what day of |
| 65 | +the week that happens to be, you can specify `?` in the |
| 66 | +`day_of_week` field. |
| 67 | + |
| 68 | +`-`:: |
| 69 | +A range of values (inclusive). Use to separate a minimum |
| 70 | +and maximum value. For example, if you want the schedule |
| 71 | +to trigger every hour between 9:00 a.m. and 5:00 p.m., you |
| 72 | +could specify `9-17` in the `hours` field. |
| 73 | + |
| 74 | +`,`:: |
| 75 | +Multiple values. Use to separate multiple values for a |
| 76 | +field. For example, if you want the schedule to trigger |
| 77 | +every Tuesday and Thursday, you could specify `TUE,THU` |
| 78 | +in the `day_of_week` field. |
| 79 | + |
| 80 | +`/`:: |
| 81 | +Increment. Use to separate values when specifying a time |
| 82 | +increment. The first value represents the starting point, |
| 83 | +and the second value represents the interval. For example, |
| 84 | +if you want the schedule to trigger every 20 minutes |
| 85 | +starting at the top of the hour, you could specify `0/20` |
| 86 | +in the `minutes` field. Similarly, specifying `1/5` in |
| 87 | +`day_of_month` field will trigger every 5 days starting on |
| 88 | +the first day of the month. |
| 89 | + |
| 90 | +`L`:: |
| 91 | +Last. Use in the `day_of_month` field to mean the last day |
| 92 | +of the month--day 31 for January, day 28 for February in |
| 93 | +non-leap years, day 30 for April, and so on. Use alone in |
| 94 | +the `day_of_week` field in place of `7` or `SAT`, or after |
| 95 | +a particular day of the week to select the last day of that |
| 96 | +type in the month. For example `6L` means the last Friday |
| 97 | +of the month. You can specify `LW` in the `day_of_month` |
| 98 | +field to specify the last weekday of the month. Avoid using |
| 99 | +the `L` option when specifying lists or ranges of values, |
| 100 | +as the results likely won't be what you expect. |
| 101 | + |
| 102 | +`W`:: |
| 103 | +Weekday. Use to specify the weekday (Monday-Friday) nearest |
| 104 | +the given day. As an example, if you specify `15W` in the |
| 105 | +`day_of_month` field and the 15th is a Saturday, the |
| 106 | +schedule will trigger on the 14th. If the 15th is a Sunday, |
| 107 | +the schedule will trigger on Monday the 16th. If the 15th |
| 108 | +is a Tuesday, the schedule will trigger on Tuesday the 15th. |
| 109 | +However if you specify `1W` as the value for `day_of_month`, |
| 110 | +and the 1st is a Saturday, the schedule will trigger on |
| 111 | +Monday the 3rd--it won't jump over the month boundary. You |
| 112 | +can specify `LW` in the `day_of_month` field to specify the |
| 113 | +last weekday of the month. You can only use the `W` option |
| 114 | +when the `day_of_month` is a single day--it is not valid |
| 115 | +when specifying a range or list of days. |
| 116 | + |
| 117 | +`#`:: |
| 118 | +Nth XXX day in a month. Use in the `day_of_week` field to |
| 119 | +specify the nth XXX day of the month. For example, if you |
| 120 | +specify `6#1`, the schedule will trigger on the first |
| 121 | +Friday of the month. Note that if you specify `3#5` and |
| 122 | +there are not 5 Tuesdays in a particular month, the |
| 123 | +schedule won't trigger that month. |
| 124 | + |
| 125 | +[[cron-expression-examples]] |
| 126 | +==== Examples |
| 127 | + |
| 128 | +[[cron-example-daily]] |
| 129 | +===== Setting daily triggers |
| 130 | + |
| 131 | +`0 5 9 * * ?`:: |
| 132 | +Trigger at 9:05 a.m. UTC every day. |
| 133 | + |
| 134 | +`0 5 9 * * ? 2020`:: |
| 135 | +Trigger at 9:05 a.m. UTC every day during the year 2020. |
| 136 | + |
| 137 | +[[cron-example-range]] |
| 138 | +===== Restricting triggers to a range of days or times |
| 139 | + |
| 140 | +`0 5 9 ? * MON-FRI`:: |
| 141 | +Trigger at 9:05 a.m. UTC Monday through Friday. |
| 142 | + |
| 143 | +`0 0-5 9 * * ?`:: |
| 144 | +Trigger every minute starting at 9:00 a.m. UTC and ending at 9:05 a.m. UTC every day. |
| 145 | + |
| 146 | +[[cron-example-interval]] |
| 147 | +===== Setting interval triggers |
| 148 | + |
| 149 | +`0 0/15 9 * * ?`:: |
| 150 | +Trigger every 15 minutes starting at 9:00 a.m. UTC and ending at 9:45 a.m. UTC every day. |
| 151 | + |
| 152 | +`0 5 9 1/3 * ?`:: |
| 153 | +Trigger at 9:05 a.m. UTC every 3 days every month, starting on the first day of the month. |
| 154 | + |
| 155 | +[[cron-example-day]] |
| 156 | +===== Setting schedules that trigger on a particular day |
| 157 | + |
| 158 | +`0 1 4 1 4 ?`:: |
| 159 | +Trigger every April 1st at 4:01 a.m. UTC. |
| 160 | +`0 0,30 9 ? 4 WED`:: |
| 161 | +Trigger at 9:00 a.m. UTC and at 9:30 a.m. UTC every Wednesday in the month of April. |
| 162 | + |
| 163 | +`0 5 9 15 * ?`:: |
| 164 | +Trigger at 9:05 a.m. UTC on the 15th day of every month. |
| 165 | + |
| 166 | +`0 5 9 15W * ?`:: |
| 167 | +Trigger at 9:05 a.m. UTC on the nearest weekday to the 15th of every month. |
| 168 | + |
| 169 | +`0 5 9 ? * 6#1`:: |
| 170 | +Trigger at 9:05 a.m. UTC on the first Friday of every month. |
| 171 | + |
| 172 | +[[cron-example-last]] |
| 173 | +===== Setting triggers using last |
| 174 | + |
| 175 | +`0 5 9 L * ?`:: |
| 176 | +Trigger at 9:05 a.m. UTC on the last day of every month. |
| 177 | + |
| 178 | +`0 5 9 ? * 2L`:: |
| 179 | +Trigger at 9:05 a.m. UTC on the last Monday of every month. |
| 180 | + |
| 181 | +`0 5 9 LW * ?`:: |
| 182 | +Trigger at 9:05 a.m. UTC on the last weekday of every month. |
0 commit comments