表达式说明如下
# 文件格式說明
# ┌──分鐘(0 - 59)
# │ ┌──小時(0 - 23)
# │ │ ┌──日(1 - 31)
# │ │ │ ┌─月(1 - 12)
# │ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
# │ │ │ │ │
# * * * * * 被執行的命令
可以在
https://crontab.guru/#0_4_8-14_*_*
查看上图,检查你的表达式是否正确
go get github.com/robfig/cron/v3
package main
import (
"fmt"
"github.com/robfig/cron/v3"
"time"
)
func TestCron() {
c := cron.New()
i := 1
EntryID, err := c.AddFunc("*/1 * * * *", func() {
fmt.Println(time.Now(), "每分钟执行一次", i)
i++
})
fmt.Println(time.Now(), EntryID, err)
c.Start()
time.Sleep(time.Minute * 5)
}
func main() {
TestCron()
}
2021-09-06 10:26:32.7499139 +0800 CST m=+0.003000001 1
2021-09-06 10:27:00.0009139 +0800 CST m=+27.254000001 每分钟执行一次 1
2021-09-06 10:28:00.0009139 +0800 CST m=+87.254000001 每分钟执行一次 2
2021-09-06 10:29:00.0009139 +0800 CST m=+147.254000001 每分钟执行一次 3
2021-09-06 10:30:00.0009139 +0800 CST m=+207.254000001 每分钟执行一次 4
2021-09-06 10:31:00.0009139 +0800 CST m=+267.254000001 每分钟执行一次 5
cron.WithSeconds()
cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger))
cron.WithLogger(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags)))
package main
import (
"fmt"
"github.com/robfig/cron/v3"
"log"
"os"
"time"
)
func TestCron() {
c := cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger)), cron.WithLogger(
cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))
i := 1
EntryID, err := c.AddFunc("*/5 * * * * *", func() {
fmt.Println(time.Now(), "每5s一次----------------", i)
time.Sleep(time.Second *6)
i++
})
fmt.Println(time.Now(), EntryID, err)
c.Start()
time.Sleep(time.Second * 30)
}
func main() {
TestCron()
}
time.sleep
中,便跳过了->2021-09-06 10:55:44.5709139 +0800 CST m=+0.003000001 1
cron: 2021/09/06 10:55:44 start
cron: 2021/09/06 10:55:44 schedule, now=2021-09-06T10:55:44+08:00, entry=1, next=2021-09-06T10:55:45+08:00
cron: 2021/09/06 10:55:45 wake, now=2021-09-06T10:55:45+08:00
cron: 2021/09/06 10:55:45 run, now=2021-09-06T10:55:45+08:00, entry=1, next=2021-09-06T10:55:50+08:00
2021-09-06 10:55:45.0009139 +0800 CST m=+0.433000001 每5s一次---------------- 1
cron: 2021/09/06 10:55:50 wake, now=2021-09-06T10:55:50+08:00
cron: 2021/09/06 10:55:50 run, now=2021-09-06T10:55:50+08:00, entry=1, next=2021-09-06T10:55:55+08:00
cron: 2021/09/06 10:55:55 wake, now=2021-09-06T10:55:55+08:00
cron: 2021/09/06 10:55:55 run, now=2021-09-06T10:55:55+08:00, entry=1, next=2021-09-06T10:56:00+08:00
2021-09-06 10:55:55.0009139 +0800 CST m=+10.433000001 每5s一次---------------- 2
cron: 2021/09/06 10:56:00 wake, now=2021-09-06T10:56:00+08:00
cron: 2021/09/06 10:56:00 run, now=2021-09-06T10:56:00+08:00, entry=1, next=2021-09-06T10:56:05+08:00
cron: 2021/09/06 10:56:05 wake, now=2021-09-06T10:56:05+08:00
cron: 2021/09/06 10:56:05 run, now=2021-09-06T10:56:05+08:00, entry=1, next=2021-09-06T10:56:10+08:00
2021-09-06 10:56:05.0009139 +0800 CST m=+20.433000001 每5s一次---------------- 3
cron: 2021/09/06 10:56:10 wake, now=2021-09-06T10:56:10+08:00
cron: 2021/09/06 10:56:10 run, now=2021-09-06T10:56:10+08:00, entry=1, next=2021-09-06T10:56:15+08:00
AddJob
:package main
import (
"fmt"
"github.com/robfig/cron/v3"
"time"
)
type Job1 struct {
}
func (t Job1) Run() {
fmt.Println(time.Now(), "I'm Job1")
}
type Job2 struct {
}
func (t Job2) Run() {
fmt.Println(time.Now(), "I'm Job2")
}
func TestCron() {
c := cron.New(cron.WithSeconds())
EntryID, err := c.AddJob("*/5 * * * * *", Job1{})
fmt.Println(time.Now(), EntryID, err)
EntryID, err = c.AddJob("*/10 * * * * *", Job2{})
fmt.Println(time.Now(), EntryID, err)
c.Start()
time.Sleep(time.Second * 30)
}
func main() {
TestCron()
}
2021-09-06 11:14:19.5669139 +0800 CST m=+0.003000001 1
2021-09-06 11:14:19.5959139 +0800 CST m=+0.032000001 2
2021-09-06 11:14:20.0009139 +0800 CST m=+0.437000001 I'm Job2
2021-09-06 11:14:20.0009139 +0800 CST m=+0.437000001 I'm Job1
2021-09-06 11:14:25.0009139 +0800 CST m=+5.437000001 I'm Job1
2021-09-06 11:14:30.0009139 +0800 CST m=+10.437000001 I'm Job2
2021-09-06 11:14:30.0009139 +0800 CST m=+10.437000001 I'm Job1
2021-09-06 11:14:35.0009139 +0800 CST m=+15.437000001 I'm Job1
2021-09-06 11:14:40.0009139 +0800 CST m=+20.437000001 I'm Job1
2021-09-06 11:14:40.0009139 +0800 CST m=+20.437000001 I'm Job2
2021-09-06 11:14:45.0009139 +0800 CST m=+25.437000001 I'm Job1