当前位置:网站首页>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()
}边栏推荐
- Discussion sur la modélisation de la série 143
- Dumi builds a document blog
- 力扣刷题338.比特位计数
- Applet waterfall flow
- 建模雜談系列143 數據處理、分析與决策系統開發的梳理
- Uni app dynamic add style dynamic bind background image invalid
- php开发14 友情链接模块的编写
- The problem that ionic3 cannot automatically install the APK package
- 120. 三角形最小路径和-动态规划
- Ctfshow SQL injection (231-253)
猜你喜欢

120. triangle minimum path sum - Dynamic Planning

Sword finger offer 56 - I. number of occurrences in the array

Dumi construit un blog documentaire

Small program input element moving up

C盘无损移动文件

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets

Redis master-slave replication, sentinel mode, cluster

【Flutter 问题系列第 67 篇】在 Flutter 中使用 Get 插件在 Dialog 弹窗中不能二次跳转路由问题的解决方案

Redis

【自动化测试】关于unittest你需要知道的事
随机推荐
Hugo blog building tutorial
Discussion sur la modélisation de la série 143
Dumi builds a document blog
Vercel 使用 HTTP 缓存
Dumi construit un blog documentaire
VGA display based on de2-115 platform
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
SS selector
EMC整改纲要
用navicat将mysql数据转化为postgresql
Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity
This Sedata uses multiple methods to dynamically modify objects and values in arrays. Object calculation properties
Latex operation
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
力扣刷题338.比特位计数
Redis
Small program input element moving up
Ctfshow common postures (821-830)
CTFSHOW 常用姿势篇(821-830)
剑指 Offer 11. 旋转数组的最小数字-二分查找