Cron Job不会通过TimeZone触发 [英] Cron Job doesn't trigger with TimeZone

查看:93
本文介绍了Cron Job不会通过TimeZone触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been doing some test with cron jobs with timezones. My goal is to setup 1 cron job per timezone so it can be run independently of the server location.

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 PM)

Here i'm just trying to make a test. I expect to see helloWorld at 15:26 Tokyo time. ( 3:26PM )

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

The sig snippet is something i've found on stack overflow for testing purposes so that the program doesn't close before the cron job has time to run.

我总是将时间设置为提前1或2分钟,但它永远都不会起作用。
知道为什么吗?

I'm always setting up the time 1 or 2 minutes ahead and it's never working. any idea why ?

推荐答案

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

I was an error of the cron formula "26 15 * * *" should have been "00 26 15 * *"

这篇关于Cron Job不会通过TimeZone触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆