go cron cron job

Copyright: by DongBao https://blog.csdn.net/aaaadong/article/details/91382660

illustrates cron

Executed once every 5 seconds: * / 5 * * * *?
Executed once every minute: 0 * / 1 * * *?
23:00 executed once a day: 0023 * *?
1:00 executed once a day: 001 * *?
Monthly No. 1 1:00 execution time: 0011 *?
In 26 points, 29 points, 33 points is performed once: 0 26,29,33 * * *?
Daily 0:00, 13:00, 18:00, 21:00 executed once: 00 0,13,18,21 * *?

Download and install

Console input go get github.com/robfig/cron Go to download regular tasks of the package, provided that you have configured the $ GOPATH

Practical application

package main

import (
	"fmt"
	"github.com/robfig/cron"
	"time"
)

func main() {
	c := cron.New()
	err := c.AddFunc(`0/10 * * * * *`, testToDo())
	if err != nil {
		fmt.Println("err", err.Error())
	}
	//特殊用法
	err = c.AddFunc(`@hourly`, testToDo())
	c.Start()
	time.Sleep(time.Minute)
}

func testToDo() func() {
	count := 1
	return func() {
		now := time.Now()
		fmt.Println("现在时间", now, ",第", count, "次")
		count++
	}
}

operation result

Reference material

https://www.cnblogs.com/zuxingyu/p/6023919.html

Guess you like

Origin blog.csdn.net/aaaadong/article/details/91382660