当前位置:网站首页>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()
}边栏推荐
- Vercel 使用 HTTP 缓存
- 2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
- Discussion sur la modélisation de la série 143
- PHP development 14 compilation of friendship link module
- The WebView case of flutter
- Express scaffold creation
- PHP security development 15 user password modification module
- SEO specification
- This Sedata uses multiple methods to dynamically modify objects and values in arrays. Object calculation properties
- 建模雜談系列143 數據處理、分析與决策系統開發的梳理
猜你喜欢

Collection of wrong questions in soft test -- morning questions in the first half of 2011

Use ASE encryption and decryption cache encapsulation in Vue project

CreateAnonymousThreadX给匿名线程传递参数

Ultra quicksort reverse sequence pair

knife4j aggregation 2.0.9支持路由文档自动刷新

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

Express framework knowledge - Art template template, cookie, session

Simple static web page + animation (small case)

电磁兼容常用名词术语

R: Employee turnover forecast practice
随机推荐
120. 三角形最小路径和-动态规划
CreateAnonymousThreadX给匿名线程传递参数
Modeling discussion series 143 data processing, analysis and decision system development
SQL 进阶挑战(1 - 5)
Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error
第三方评论插件
使用Service Worker优选请求资源 - 持续更新
Knife4j aggregation 2.0.9 supports automatic refresh of routing documents
February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
【Try to Hack】upload-labs通关(暂时写到12关)
Redis主从复制、哨兵模式、集群
剑指 Offer 56 - I. 数组中数字出现的次数
Implementation of article list function on PHP 18 home page
CTFSHOW 常用姿势篇(821-830)
XOR prefix and +map maintenance
Ctfshow SQL injection (231-253)
Clear timer failure
php 18 首页的文章列表功能实现
Zoom and move the H5 part of the mobile end
【自动化测试】关于unittest你需要知道的事