Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cron does not support system time change?? #154

Open
jilieryuyi opened this issue Sep 11, 2018 · 7 comments
Open

cron does not support system time change?? #154

jilieryuyi opened this issue Sep 11, 2018 · 7 comments

Comments

@jilieryuyi
Copy link

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

@ooopSnake
Copy link

have same problem too.
any suggestions ?

1 similar comment
@xkfen
Copy link

xkfen commented Jun 3, 2019

have same problem too.
any suggestions ?

@robfig
Copy link
Owner

robfig commented Jun 3, 2019

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?

@xkfen
Copy link

xkfen commented Jun 5, 2019

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")
}
}

@kjcxmx
Copy link

kjcxmx commented Sep 20, 2019

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

@mohakshah
Copy link

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.

@wizardforcel
Copy link

wizardforcel commented Jun 29, 2021

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)
	}
}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants