在go裏像crontab一樣,實現你的定時任務

下載

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看這個

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章