What's new

Delaying the connection to the NTP server ?

  • 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!

Asusme

New Around Here
Hi,

I have a ASUS RT-AC66U with merlin firmware 378.56_2 and a separate DSL modem.

Occasionally it happens that I have power outages. Once power is restored, the router is unable to connect to the NTP server when it starts because the modem has not yet established the internet connection (it needs a period of about one minute to accomplish this). So I constantly have the wrong time in my interface unless I start the router after the modem has established a connection.

Is there a way to delay the connection to the NTP server with a script ? How ?

Thanks for your help.
 
Here's a sample code. Not only has it no been tested, but I'm not even running 378.56_2 yet.

What this script does: When the Internet comes back, this script immediately kills the NTP client and then waits 60 seconds before restarting it.

/jffs/scripts/wan-start
Code:
###################################################################
# immediately disable the router's NTP client program
killall ntp
sleep 1
nvram set ntp_ready=1
nvram unset ntp_server0
nvram unset ntp_server1
nvram unset ntp_server_tried
# nvram commit
###################################################################


###################################################################
# background delayed start of the router's NTP client program
(
sleep 60
nvram unset ntp_ready
nvram set ntp_server0=pool.ntp.org
nvram set ntp_server1=2.pool.ntp.org
# nvram commit
service restart_ntpc
) &
###################################################################
 
Last edited:
This really shouldn't be necessary....setting the time is a critical function and it's retried forever if it fails. As part of the retries, it first checks if the WAN connection is up. More likely there is a bug in refreshing that particular WAN status var when the modem comes up after the router.
 
Here's a sample code. Not only has it no been tested, but I'm not even running 378.56_2 yet.

What this script does: When the Internet comes back, this script immediately kills the NTP client and then waits 60 seconds before restarting it.

/jffs/scripts/wan-start
Code:
###################################################################
# immediately disable the router's NTP client program
killall ntp
sleep 1
nvram set ntp_ready=1
nvram unset ntp_server0
nvram unset ntp_server1
nvram unset ntp_server_tried
nvram commit
###################################################################


###################################################################
# background delayed start of the router's NTP client program
(
sleep 60
nvram unset ntp_ready
nvram set ntp_server0=pool.ntp.org
nvram set ntp_server1=2.pool.ntp.org
nvram commit
service restart_ntpc
) &
###################################################################

Quick comments on your suggested script:

Why killall NTP at start? Does it hurt anything to let it try to get the time?

Why the nvram commit lines? They preserve settings over a reboot, but this script will be run again on reboot so commit lines should not be needed.
 
... a separate DSL modem.

Occasionally it happens that I have power outages. Once power is restored...

Check that you have "DHCP query frequency" set to normal mode, not aggressive. I believe that normal makes the start up process wait for 120 seconds before doing dhcp to handle slow devices on power up get their connection stable. I am on a DSL connection, but not your version of the firmware.
 
Check that you have "DHCP query frequency" set to normal mode, not aggressive. I believe that normal makes the start up process wait for 120 seconds before doing dhcp to handle slow devices on power up get their connection stable. I am on a DSL connection, but not your version of the firmware.
I set up a static IP for my WAN so this setting is not available.
 
Why killall NTP at start?
There not enough information to know what the problem is.

Could be that his DNS is not ready. And that this is causing a 3-4 minute delay in setting the clock to the correct time. So, my sample code is not a valid solution.

A better way might be to resolve the IP addresses for pool.ntp.org ahead of time, then cache them for use when the Internet comes back. Then it does not matter if the DNS is ready because you will use the cached IP addresses of pool.ntp.org, instead of the name.

My RT-AC68U sets the clock from GPS using NTPd. Eventually I want to remove the Asus NTP client from the firmware because I don't need it anymore.
 
Here is my little rant..I'm in the same boat as ASAT. I don't run NTP on my AC56U but a stripped down and yet fully functional and standard compliant NTP daemon.

The Asus so-called NTP is an extremely dumb program which in simple term is a timer. When the time comes (roughly twice per day...a bit less simple than that in reality), it run ntpclient to sync router's clock with a time server.

While ntpclient is standard compliant, Asus NTP + ntpclient is by no means a real NTP daemon. And yet it consumes more resource and creates issues from time to time over the last few years. Simply search SNB for NTP...

If I were Asus, I would have killed the combination of Asus NTP + ntpclient now. Replace with a stripped down but fully functional NTP daemon...and plus a few nice looking charts.

Flash memory is no longer a scarce resource in their current and future models.
 
  • Like
Reactions: GWB
For OP's issue or any pre-mature failure of Asus NTP, try adding the following lines to /jffs/scripts/wan-start
Code:
nvram set ntp_ready=0
ntpclient -s -h $(nvram get ntp_server0)
service restart_ntpc

And if you suspect NTP issue related to DNS not ready (e.g. guys moaning in the DnsCrypt thread), try putting IP address instead of hostname. You bypass one layer of redirection and avoid problem. From command line,

Code:
nvram set ntp_server0=1.2.3.4
nvram set ntp_server1=4.3.2.1
nvram commit

Note the IPs are bogus. Replace with your own.
 
I am amazed that how much people love to abuse nvram.

nvram has write lifetime, and is pretty hard to replace when it dies.
 
Why don't you simply get a cheap UPS?


Sent using Tapatalk
 
Here's a sample code. Not only has it no been tested, but I'm not even running 378.56_2 yet.

What this script does: When the Internet comes back, this script immediately kills the NTP client and then waits 60 seconds before restarting it.

/jffs/scripts/wan-start
Code:
###################################################################
# immediately disable the router's NTP client program
killall ntp
sleep 1
nvram set ntp_ready=1
nvram unset ntp_server0
nvram unset ntp_server1
nvram unset ntp_server_tried
# nvram commit
###################################################################


###################################################################
# background delayed start of the router's NTP client program
(
sleep 60
nvram unset ntp_ready
nvram set ntp_server0=pool.ntp.org
nvram set ntp_server1=2.pool.ntp.org
# nvram commit
service restart_ntpc
) &
###################################################################

For OP's issue or any pre-mature failure of Asus NTP, try adding the following lines to /jffs/scripts/wan-start
Code:
nvram set ntp_ready=0
ntpclient -s -h $(nvram get ntp_server0)
service restart_ntpc

And if you suspect NTP issue related to DNS not ready (e.g. guys moaning in the DnsCrypt thread), try putting IP address instead of hostname. You bypass one layer of redirection and avoid problem. From command line,

Code:
nvram set ntp_server0=1.2.3.4
nvram set ntp_server1=4.3.2.1
nvram commit

Note the IPs are bogus. Replace with your own.

Is this the best way to add some redundant NTP ip's to use at boot? I am using dnscrypt, and would like to add a couple more NTP ip addresses, it looks like the gui only allows for one, but your scripts look like you can have more.
Does adding the NTP ip address's in vpn routing policy to use WAN make sense if the vpn uses a domain name and not a ip address? I am just trying to make sure I don't have a weak spot later (like a ntp server down causing dnscrypt to fail ...causing vpn client failing to get ip of server...) on when I am away from the router physically for long durations.
EDIT: well on further discovery, it seems the router's ip (example- 192.168.0.1) does not use the vpn. (using the GUI's traceroute google.com, and comparing it to my computers mtr google.com) The router's packets go through the WAN while my computer goes to the VPN like it should. I specified the router's ip to use the VPN which I thought might lock me out after a reboot, but it did'nt. So I am not sure what to think of that. lol
 
Last edited:
Is this the best way to add some redundant NTP ip's to use at boot? I am using dnscrypt, and would like to add a couple more NTP ip addresses, it looks like the gui only allows for one, but your scripts look like you can have more.

The firmware allows a maximum of two IP addresses (the GUI only exposes one for editing) that will be used in a round robin fashion.

Two are sufficient for most users. There are much weaker link in the implementation of the stock NTP. For more demanding users, I would recommend a custom NTPD.
 
Sorry for the delay..
I'm still having the issue.. When my ISP modem has established the connection after my router has completed its boot process, the router can't synchronize the date and time with the NTP server (I have to restart it once the connection is established).

My log shows many entries like :
Aug 1 02:08:48 hour monitor: daemon is starting
Aug 1 02:08:48 hour monitor: ntp is not syn
...
 
I'm still having the issue..
If nothing else works, try this. It will use JFFS to save the last clock value between reboots of the router.

First, set the clock manually. The format is YYYYMMDDhhmm.ss

Example

Code:
/bin/date -s 201601231730.00
/usr/bin/killall ntp && /bin/sleep 1 && /usr/sbin/nvram set ntp_ready=1


Then, create this scripts.

/jffs/scripts/init-start
Code:
#!/bin/sh
[ -f /jffs/clock-seconds-saved ] && /bin/date -u -s 197001010000.$(($(cat /jffs/clock-seconds-saved) + 1))


/jffs/scripts/services-stop
Code:
#!/bin/sh
[ "$(/usr/sbin/nvram get ntp_ready)" == "1" ] && /bin/date -u +%s > /jffs/clock-seconds-saved


And, enable the scripts.
Code:
/bin/chmod a+x /jffs/scripts/init-start /jffs/scripts/services-stop


Lastly, this technique separates NTP clock synchronization from the DNS lookup of the NTP server name. However, it was not tested.
http://www.snbforums.com/threads/dnscrypt-from-opendns.11645/page-18#post-229064
 
Last edited:
If nothing else works, try this. It will use JFFS to save the last clock value between reboots of the router

Custom hw-faXeclock!

Would adding a calibrated offset to the saved value increase the accuracy on soft reboot?
 
If nothing else works, try this. It will use JFFS to save the last clock value between reboots of the router.

First, set the clock manually. The format is YYYYMMDDhhmm.ss

Example

Code:
/bin/date -s 201601231730.00
/usr/bin/killall ntp && /bin/sleep 1 && /usr/sbin/nvram set ntp_ready=1

Then, create this scripts.
Do I specify the current time/date including seconds ? How do I go back if it doesn't work ? The 2nd script is run before all system services are stopped but what if the router suddenly turns off due to a power outage ?
 
Last edited:
Do I specify the current time/date including seconds ? How do I go back?
You can manually set the system clock to the current date/time with the Linux command /bin/date. Then you must tell the router that you have successfully synchronized the system clock.

The scripts are designed to save/restore the clock using the number of seconds since midnight Jan. 1 1970 UTC. I think it is this way because I originally had it using the Linux command /bin/touch to store the current date/time as a filesystem timestamp of a zero byte file. To summarize what's going on:

Element 1 - Bootstrapping the system. <==YOU ARE HERE

Element 2 - Precise clock synchronization via GPS or Internet.
 
Yes but the actual time changes twice a year, in late march and again in late october, by one hour.
 
guys, I think you are on the wrong track. There are some good suggestions above..e.g. the one from me (cough cough).

One of them should solve your problem. If it doesn't, you probably want to understand why instead of randomly trying ever increasingly exotic workaround and solution.

When WAN is up, sync the clock. It's a simple task...
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

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