Delay service start until NTP time sync

  • ATTENTION! As of November 1, 2020, you are not able to reply to threads 6 months after the thread is opened if there are more than 500 posts in the thread.
    Threads will not be locked, so posts may still be edited by their authors.
    Just start a new thread on the topic to post if you get an error message when trying to reply to a thread.


New Around Here

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

I'm thinking to add this to /jffs/scripts/init-start
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?



Part of the Furniture
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.


Occasional Visitor
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.

Similar threads

Sign Up For SNBForums Daily Digest

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