Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write cron expression

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to write cron expressions". In daily operation, I believe many people have doubts about how to write cron expressions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to write cron expressions". Next, please follow the editor to study!

The cron expression is a string that is divided into seven fields by six spaces, each representing a unit of time. The format is as follows:

[seconds] [minutes] [hours] [day] [month] [week] [year]

Usually the part that defines "year" can be omitted, but the part that is commonly used actually consists of the first six parts.

Definition of each part of cron

The definitions of the various fields for cron are shown in the following table:

Is the field required and the range wildcard second is 0-59,-* / min is 0-59,-* / hour is 0-23,-* / day is 1-31,-*? / L W month is 1-12 or JAN-DEC,-* / week is 1-7 or SUN-SAT,-*? / L # No 1970-2099 in-* / cron

This refers to execution at more than two time points, and if we define it as 8 minutes 12 minutes 35 in the "score" field, we will perform the scheduled task at 8 points and 12 minutes and 35 minutes respectively.

-this is easy to understand is to specify a continuous range in a certain domain. If we define 1-6 in the "hour" field, it means that it is triggered every hour between 1 and 6 o'clock.

* represents all values, which can be interpreted as "every". If you set * in the "day" field, it will be triggered every day.

? Indicates that no value is specified. The scenario used is that you do not need to care about the value of this field currently set. For example: to trigger an action on the 8th of each month, but don't care what day it is, we can set 008 * like this?

Triggered periodically on a domain, the symbol divides the expression in its domain into two parts, the first part being the starting value, which is reduced by one unit except seconds. For example, defining 5 Universe 10 on "second" means that it is executed every 10 seconds from the 5th second, while on "minute" it means that it is executed every 10 minutes from the 5th second.

L means LAST in English and can only be used in "day" and "week". Set in "Day" to indicate the last day of the month (depending on the current month, February will also be based on whether it is a run year), and on "week" it means Saturday, which is equivalent to "7" or "SAT". If you add a number before "L", it represents the last of the data. For example, setting the format "7L" in "week" means "the last Saturday of this month"

W means that the nearest working day from the specified date (Monday to Friday) is triggered and can only be used in the "day" and only after a specific number. If you set "15W" on "Day", it means that the working day closest to the 15th of each month is triggered. If the 15th happens to be Saturday, then find the nearest Friday (14th) to trigger, if the 15th is the weekend, then find the nearest next Monday (16th) to trigger. If the 15th happens to be on a weekday (Monday to Friday), it will be triggered on that day. If it is "1W", it can only be pushed to the next nearest working day of this month, not across the month to the previous month.

# indicates the day ordinal of each month and can only be used in "week". For example, "2-3" means on the third Tuesday of each month.

Example

Here are some examples that can be practiced according to the above explanation:

Execute every 1 minute: 0 * / 1 *?

Execute it at 23:00 every day: 0.023 *?

Execute at 1: 00 a. M. on the 1st of each month: 0 011 *?

Executed at 23:00 on the last day of each month: 0.023 L *?

It will be implemented every Sunday at 1: 00 a. M.: 01? * L

Execute once at 26 points, 29 points, 33 points: 0. 26, 29, 33 *?

At this point, the study of "how to write cron expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report