当前位置:网站首页>Go scheduled task cron package usage
Go scheduled task cron package usage
2022-06-13 04:33:00 【gohxc】
cron What is it?
cron It means : Planning tasks , To put it bluntly, it's a timed task . I have an appointment with the system , You run a task at what time, minutes, seconds or every few minutes (job), It's that simple .
Download and install
go get github.com/robfig/cron
cron expression
cron Expressions are a good thing , This thing not only Java Of quartZ Can use ,Go Language can also be used . I haven't used Linux Of cron, But online says Linux It can also be used crontab -e Command to configure scheduled tasks .Go Language and Java Both are accurate to seconds , however Linux Not in the middle .
cron The expression represents a collection of times , Use 6 Space separated fields represent :
Field name Whether must Allowed value Allowed specific characters
second (Seconds) yes 0-59 * / , -
branch (Minute) yes 0-59 * / , -
when (Hours) yes 0-23 * / , -
Japan (Day of month) yes 1-31 * / , - ?
month (Month) yes 1-12 or JAN-DEC * / , -
week (Day of week) no 0-6 or SUM-SAT * / , - ?
notes :
- 1. month (Month) And week (Day of week) Field values are not case sensitive , Such as :SUN、Sun and sun It's the same .
- 2. week (Day of week) If the field is not provided , Equivalent to *
cron Specific character description
1) asterisk (*)
Express cron The expression can match all the values of the field . As in the 5 Use an asterisk for each field (month), It means every month
2) Oblique line (/)
Represents the growth interval , As the first 1 A field (minutes) The value is 3-59/15, Means the... Of every hour 3 Minutes to start , After every 15 Once per minute ( namely 3、18、33、48 At these points in time ), It can also be expressed here as :3/15
3) comma (,)
Used to enumerate values , As the first 6 The first field value is MON,WED,FRI, Express Monday 、 3、 ... and 、 5、 ... and perform
4) hyphen (-)
Represents a range , As the first 3 The value of the first field is 9-17 Express 9am To 5pm Directly every hour ( Include 9 and 17)
5) question mark (?)
Used only for Japan (Day of month) and week (Day of week), Indicates that no value is specified , Can be used instead of *
cron Illustrate with examples
every other 5 Once per second :*/5 * * * * ?
every other 1 Once per minute :0 */1 * * * ?
Every day 23 Click to execute once :0 0 23 * * ?
Every morning 1 Click to execute once :0 0 1 * * ?
monthly 1 No. In the morning 1 Click to execute once :0 0 1 1 * ?
stay 26 branch 、29 branch 、33 Execute once :0 26,29,33 * * * ?
Daily 0 spot 、13 spot 、18 spot 、21 Do it all once :0 0 0,13,18,21 * * ?
Project use
package main
import (
"github.com/robfig/cron"
"log"
)
func main() {
i := 0
c := cron.New()
spec := "*/5 * * * * ?"
c.AddFunc(spec, func() {
i++
log.Println("cron running:", i)
})
c.Start()
}
边栏推荐
- php开发14 友情链接模块的编写
- Li Kou brush question 338 Bit count
- 建模雜談系列143 數據處理、分析與决策系統開發的梳理
- Dumi construit un blog documentaire
- It's the Caesar code. (*‘▽‘*)*
- 一致性哈希的简单认识
- 一款開源的Markdown轉富文本編輯器的實現原理剖析
- Powershell 加域 Add-Computer模块
- PHP development 16 exit module
- PHP security development 15 user password modification module
猜你喜欢
Dumi builds a document blog
剑指 Offer 56 - I. 数组中数字出现的次数
记录一次排查问题的经过——视频通话无法接起
[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter
Hugo blog building tutorial
Analysis of the implementation principle of an open source markdown to rich text editor
[automated test] what you need to know about unittest
Ctfshow SQL injection (211-230)
Explanation of line segment tree
2019 Blue Bridge Cup
随机推荐
基于DE2-115平台的VGA显示
MySQL index
使用Service Worker优选请求资源 - 持续更新
Ctfshow SQL injection (231-253)
Day45. data analysis practice (1): supermarket operation data analysis
在线音频调节技术汇总
Express scaffold creation
El expression
Sword finger offer 11 Minimum number of rotation array - binary lookup
JS common array methods
记录一次排查问题的经过——视频通话无法接起
PHP development 14 compilation of friendship link module
Explanation of line segment tree
php 18 首页的文章列表功能实现
Redis master-slave replication, sentinel mode, cluster
Redis主从复制、哨兵模式、集群
[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter
CTFSHOW SQL注入篇(231-253)
Use ASE encryption and decryption cache encapsulation in Vue project
Koa file upload and download