What's new

Delay service start until NTP time sync

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

M95D

New Around Here
Hi.

How can I delay service start until router's clock is synchronized?

I'm thinking to add this to /jffs/scripts/init-start
Code:
while [ `date +%Y` -lt 2020 ] ; do sleep 10 ; done
Will this work?
What is the router's clock set to when it boots? Unix epoch (1970)? Firmware build date? Or is there some mechanism that brings the clock closer to reality like in OpenWRT (clock set to a recently modified file's timestamp on jffs)?
What happens if NTP fails. Will the script sleep forever?

Thanks.
 
The WAN doesn't start until after init-start is run, so there's no chance of NTP syncing properly at that point. However, init-start does not block the startup sequence, so it wouldn't do any good anyway. The default date will be circa 05-May-2018 during boot.

The generally accepted method is to wait for "$(nvram get ntp_ready)" = "1" and then start or restart your daemon/script/etc.
 
How can I delay service start until router's clock is synchronized?

Last year, I wanted to delay calling some of my own scripts from within the "services-start" script until the system timer was showing a more current date/time stamp (NOT the initial/default boot date of "2018-May-04" on my RT-AC86U) so I ended up writing my own script to wait until either a more current date/time or a maximum delay had been reached (default 180 seconds). It usually takes between 12-20 seconds to get out of the wait loop when called from the "services-start" script, so a maximum delay of 60 seconds is what I used as the parameter for my script on my RT-AC86U router, but YMMV depending on the point within the boot sequence at which you call the script. Note that the wait loop will not hang the router indefinitely; if the maximum wait time is reached it will exit the loop even if the system timer has not synchronized with NTP service. It's been working great for me so far for more than a year without any issues.

You're free to use my script, but the usual caveat of "use at your own risk" applies.

https://pastebin.com/c5EiuVaf
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top