preface

For development purposes, we studied the cron expression and sorted it out as follows:

What is a cron?

Before we look at cron expressions, let’s talk about the crontab scheduled task. In daily development or operation and maintenance, it is common to encounter some tasks or requirements that are executed periodically. For example, one script is executed every period of time, and one command or operation is executed every Monday. Linux already provides us with a convenient way to do the crontab scheduled task; The crontab command is a customized timer. You can use the crontab command to execute specified system commands or shell scripts at fixed intervals. The time interval is written in a similar way to the cron expression we usually use. To perform operations periodically by using characters or commands to set timing.

We can think of a CRon expression as a string separated by five or six string Spaces. The five or six Spaces divide the string into six or seven fields, each representing a different meaning. The default values are “second, minute, hour, day, month, week, day, year” from left to right.

Cron content

The domain field meaning Range or wildcard If required
seconds Seconds seconds 0-59, - * / is
points Minutes minutes 0-59or, - * / is
when Hours hours 0-23or, - * / is
day Day-of-Month The date of 1-31, - *? /LW is
month Month in 1-12(orJAN-DEC) or, - * / is
week Day-of-Week The day of the week 1-7(orSUN.MON.TUE.WED.THU.FRI.SAT) or, - *? / L # is
years Year year 1970-2099.or- * / no

Meanings of special characters:

  • , : lists enumeration values. A mid-month value of 1,3 indicates that both January and march trigger events.

  • * : matches any value in the field. For example, if the field of hour is *, events are triggered every hour

  • ? : indicates that no value is specified. It is used for the mutual exclusion of the date and week fields. Usually in the date and week fields one specifies the value, and the other uses? Indicates that no value is specified. Ignore the value of this field and execute directly according to the value of another field. For example, if the date is 2, what is the day of the week? ;

  • – : indicates a range that is triggered continuously. For example, if the value of, is 9-12, it indicates that events are triggered in each of the 9-12 hours.

  • / : Indicates the interval triggering event (start time/interval). For example, if the minute value is 0/15, the event is triggered every 15 minutes from 0.

  • # : indicates the NTH week of the month x(x#n), using the week field only. Week: 6#3, the third Friday of the month.

  • The last day of the week. Appears only in the day and week fields. The day represents the last day of the month, and the week represents the last week of the month. For example, if the value of the week field is 5L, it is the Thursday of the last week in the month. Do not specify list ‘,’ or range ‘-‘ when using ‘L’, as this can lead to unexpected results.

  • W: Used only in the day field and represents the working day (Monday to Friday) closest to the given date of the month.

    For example, “4W” indicates the trigger event of the month closest to 4th. (1) When the 4th day is a working day, it means that the day is triggered; If the 4th is Saturday, the 3rd (Friday) is triggered. (2) If the 4th falls on Sunday, it means that the 5th is triggered on Monday. Another example: “1W” indicates the closest working day to the 1st to trigger the event, but this working day only counts the current month. If the first of the month falls on a Saturday, “1W” means the event is triggered on Monday the third of the month. The event is not triggered even if the last day of the previous month was a working day.

  • LW: ‘L’ and ‘W’ can be used together in the day field. Indicates the trigger event on the last working day of the month.

Use the sample

// From left to right: second minute day month week year

"30 * * * *? *"  // Triggers an event every half minute
"0 0 12? * WED"  // Indicates every Wednesday at 12 noon
"0,15 8-11 * * 2" // Every Monday between 8 am and 11 am (8, 9, 10, 11) an event is triggered at minute 0 and 15
"0 0/5 15 * *?" // Triggers every 5 minutes between 3pm and 3:55pm each day
"0 0 12 L * ?" // Trigger the event at 12:00 noon on the last day of each month
"0 0 12 4W * ?" The event is triggered at 12:00 noon on the nearest working day to the 4th of each month
"0 0 12 LW * ?" // Trigger the event at 12:00 noon on the last working day of each month
"0 0 12? * 6 # 3" // The event is triggered on the 3rd Friday of each month at 12:00 noon
"0, 11, 11, 11, 11? 2019" // It will be triggered at 11:11 am on November 11, 2019 (Singles Day)

Copy the code

Refer to the article

  • Crontab Indicates a scheduled task

  • Cron — Ruthless records

  • Cron expression explanation – AD sweet honey