每隔24小时运行的cron golang代码是什么?

huangapple go评论94阅读模式
英文:

cron golang running after every 24 Hour?

问题

cron是一个在每24小时后运行的golang程序,但是当我尝试改变系统时间时,它没有被调用。

代码:

package main;

import(
    "fmt"
    "strconv"
    "strings"
    "gopkg.in/robfig/cron.v2"
    "time"
)

func Envoke_ASSET_INFO() {    
    fmt.Println("调用 Envoke_ASSET_INFO ", time.Now())
}

func main(){
    C:=cron.New()
        
    min:=strconv.Itoa(int(17))
    h:=strconv.Itoa(int(16))

    sep:="0"+" "+min+" "+h+" "+"*"+" "+"*"+" "+"*"
    fmt.Println("传递给函数的规范:", sep)    
    C.AddFunc(sep, Envoke_ASSET_INFO )
    C.Start()
    select{}
}

当我运行这个程序时,它会调用我的函数。但是当我改变系统时间(+24小时)以检查下一次调用时,它没有发生。

英文:

cron golang is running after every 24 hours but when I am trying to change the system time, it is not invoking.

code:

package main;

import(
	"fmt"
	"strconv"
	"strings"
	"gopkg.in/robfig/cron.v2"
	"time"
	)

func Envoke_ASSET_INFO() {    
	fmt.Println("Invoking Envoke_ASSET_INFO ", time.Now())
}

func main(){
	C:=cron.New()
		
	min:=strconv.Itoa(int(17))
	h:=strconv.Itoa(int(16))

	sep:="0"+" "+min+" "+h+" "+"*"+" "+"*"+" "+"*"
	fmt.Println("SPECIFATION PASSED TO FUNCTION :", sep)	
	C.AddFunc(sep, Envoke_ASSET_INFO )
	C.Start()
	select{}
}

When I am running this program it is evoking my function. But when I change my system time (+24 hours) to check the next evoking it is not happening.

答案1

得分: 5

这不是cron的工作方式。当你改变系统时间时,cron不会运行过期的任务。想象一下,如果它按照这种方式工作,并且你在预定每5分钟运行一次的作业的2天后打开机器,会发生什么。如果你真的想以这种方式进行测试,你应该将系统时间更改为接近作业应该运行的时间,并等待看它是否运行。

就个人而言,我认为更好的做法是将小时和分钟作为参数传递,并检查作业是否在下一分钟运行或其他时间。

英文:

This is not how cron works. Cron won't run overdue tasks when you change system time. Think what would happen if it worked that way and you turned your machine on after 2 days with job scheduled to run every 5 minutes. If you really want to test it that way you should change the system time to a time just before your job is supposed to run and wait to see if it does.

Personally I think that it's a better idea to pass hour and minute as parameters and check if the job is running on next minute or something.

huangapple
  • 本文由 发表于 2016年1月2日 18:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/34565070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定