首页 > 解决方案 > Cron Job 不使用 TimeZone 触发

问题描述

我一直在对带有时区的 cron 作业进行一些测试。我的目标是为每个时区设置 1 个 cron 作业,以便它可以独立于服务器位置运行。

package main

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

func helloWorld() {
  fmt.Println("hello world")
}

func main() {
    s, err1 := cron.Parse("26 15 * * *")
    fmt.Println(err1)
    l, err := time.LoadLocation("Asia/Tokyo")
    fmt.Println(err)
    c := cron.NewWithLocation(l)
    c.Schedule(s, cron.FuncJob(helloWorld))
    c.Start()

    sig := make(chan os.Signal)
    signal.Notify(sig, os.Interrupt, os.Kill)
    <-sig
}

这里我只是想做一个测试。我希望在东京时间 15:26 看到 helloWorld。(下午 3 点 26 分)

sig 片段是我在堆栈溢出中发现的,用于测试目的,因此在 cron 作业有时间运行之前程序不会关闭。

我总是提前 1 或 2 分钟设置时间,但它从来没有工作过。知道为什么吗?

标签: gocron

解决方案


我是cron公式的错误"26 15 * * *" 应该是"00 26 15 * *"


推荐阅读