cron icon indicating copy to clipboard operation
cron copied to clipboard

cron does not support system time change??

Open jilieryuyi opened this issue 5 years ago • 7 comments

if change system time, cron do not work again!!!

jilieryuyi avatar Sep 11 '18 13:09 jilieryuyi

have same problem too. any suggestions ?

ooopSnake avatar Oct 10 '18 06:10 ooopSnake

have same problem too. any suggestions ?

xkfen avatar Jun 03 '19 13:06 xkfen

What is the behavior that you observe?

I believe that cron would run the next job at the originally-scheduled time (before the time change), and then it would schedule the next job for an accurate time (after the time change). Is that what you observe?

The only way I can think of to detect that the system time has changed is to have a dedicated goroutine that sleeps for known periods of time and compares (time at start of sleep + duration of sleep) to (time.Now). Is there any other way?

I'm not sure.. I don't perceive this as a significant-enough use case to merit the effort and complication of solving it. What is your use case, that results in this being a problem?

robfig avatar Jun 03 '19 13:06 robfig

spec := "0 0 23 * * ?" 1.when the application is running, change the system time before now, it didn't work, but if i change the system time after now, it running. for example, the real system time is 2019-06-04 22:58:59, when 2019-06-04 23:00:00, the cron run.but if i change system time to 2019-06-03 22:28:59, the cron didn't work. but if i change system time to 2019-06-05 23:58:59 or 06-06..., the cron run.

2.my application is stoped. i start my application after i changed the system time, the cron runs . for example:the real time is 2019-06-04 22:58:59, and i change it to 2019-06-03 22:58:59, and then i run my application, when 2019-06-03 23:00:00, the cron runs. that is to say, changing the time before restarting will take effect.

3.my final choice is using the time.NewTicker() ticker := time.NewTicker(time.Second) for { select { case <- ticker.C: fmt.Println("hello katy") } }

xkfen avatar Jun 05 '19 01:06 xkfen

cron is support system time change I may give you some tips. If you want set everyday when 00:01:00 the cron runs . you just set spec:="0 1 0 1/1 * ?",when now is 2019-08-15 00:00:00.so when you wait one minute the cron runs.but for next runs,You have to change the system time between the initiation of the task and the first execution.You know it work time is the cron runs next second will get now time again,then it will run in this change system time

kjcxmx avatar Sep 20 '19 10:09 kjcxmx

I had a utility that used this package set as a startup program on a raspberry pi. Since Pis do not have a hardware clock, they synchronise with the network time at booting up. My utility would be started before this synchronisation and hence would schedule jobs at the wrong time. The fix I ended up deploying was to fetch the network time in my utility and compare it with the system time before starting the cron scheduler.

Also, before deploying the fix, I did notice that as per robfig's prediction, the consecutive schedules would run at the correct times.

mohakshah avatar Feb 25 '20 09:02 mohakshah

My solution is to start another goroutine. If the system time changed greatly, it can detect it.

var last = time.Time{}

go func() {
	for {
		var tm = time.Now()
		if last.Year() != 1  {
			var diffSec =  tm.Unix() - last.Unix()
			if diffSec > 10 || diffSec < -10 {
				task.Stop()
				task.Start()
			}
		}
		last = tm
		time.Sleep(10 * time.Second)
	}
}()

wizardforcel avatar Jun 29 '21 02:06 wizardforcel