下載
go get -u -v github.com/robfig/cron
說明
1.crontab命令見https://www.runoob.com/linux/linux-comm-crontab.html
2.一個比較好用的crontab執行時間計算可以用來測試你的命令https://tool.lu/crontab/
使用
// 開始定時任務,這個會開啓協程,後臺掛起,所以保證你的進程是阻塞的
func StartCronJob() {
c := cron.New()
spec := "1 0 1,11,21 * *"
err := c.AddFunc(spec, func(){
fmt.Println(1111)
})
if err != nil {
panic(err)
return
}
c.Start()
}
其他
更多的使用方法https://github.com/robfig/cron看這個