What's new

NTP Daemon for ASUSWRT/Merlin

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

Dang. That's my big problem. Any time the router reboots ntpclient is fired up and wrecks all the clock discipline ntpd has built up over the preceding hours/days. So far it seems to help to manually kill ntp and then do an ntpd -gq and then waiting long enough for the clock slew to get the time back to correct before starting ntpd, but that's a pain.
Do you use a NTP drift file?
 
Last edited:
Asus may require you to properly kill their NTP service with a SIGTSTP signal, after your clock has been synchronized:

/usr/bin/killall -SIGTSTP ntp


OR, the quick+dirty way to fix your WebUI text:

/usr/sbin/nvram set svc_ready=1


AND, I refer you to the AsusWRT source code :)

https://github.com/RMerl/asuswrt-merlin/blob/master/release/src/router/rc/ntp.c#L47
/usr/bin/killall -SIGTSTP ntp did it!

Thanks for the link but you know by know I don't speak that language :(

where should I add (or what should I replace) with the scripts you did earlier?
 
where should I add (or what should I replace) with the scripts you did earlier?
EDIT: You should probably kill the NTP service too. The first killall sends a signal telling the NTP service to do some housekeeping(?), and the second killall terminates the process. Example:
Code:
# wait for Asus NTP service to start, if the router is booting
wait_remaining=120 # seconds
wait_interval=2
while [ $wait_remaining -gt 0 ] && [ -z "$(/bin/pidof ntp)" ] ; do
  /bin/sleep $wait_interval
  let wait_remaining-=$wait_interval
done

/bin/sleep 1

/usr/sbin/nvram set ntp_ready=1
if [ $wait_remaining -le 0 ]; then
  # if Asus NTP service is not running
  /usr/sbin/nvram set svc_ready=1
else
  # if Asus NTP service is running
  /usr/bin/killall -TSTP ntp
  /bin/sleep 7
  /usr/bin/killall -TERM ntp
  /bin/sleep 1
fi

# prevent Asus NTP service from starting
/bin/rm -f /var/run/ntp.pid
/bin/mkdir -p /var/run/ntp.pid



I put it immediately following the "set ntp_ready=1", in the above script set_ntp_ready.sh. And it's run in the background because there's waiting involved.


 
Last edited:
EDIT: You should probably kill the NTP service too. I'm not at the router to test it. The first killall sends a signal telling the NTP service to do some housekeeping(?), and the second killall terminates the process. Example:
/usr/sbin/nvram set ntp_ready=1
/usr/bin/killall -TSTP ntp
/bin/sleep 5
/usr/bin/killall -TERM ntp




I would put it immediately following the "set ntp_ready=1", in the above script set_ntp_ready.sh.

That seems to have done it, thanks again.

does the NTP Server box on the admin/system page need pointing to the NTPD server (192.168.1.1) or does it do that anyway?
 
That seems to have done it, thanks again.
The final step is to "mkdir /var/run/ntp.pid" to prevent the Asus NTP service from starting again.

... does the NTP Server box on the admin/system page need pointing to the NTPD server (192.168.1.1) or does it do that anyway?
The Asus NTP service may default it back to something, but it don't matter.
 
I tried this other way, and it works too. It is more simple.

/jffs/scripts/set_ntp_ready.sh
Code:
#!/bin/sh
wait_for_clock_change() {
  local snooze_secs=2
  local detect_secs=10
  local t_busybox="$(/bin/date -r /bin/busybox +%s)"

  while : ; do
    if [ "$(/usr/sbin/nvram get ntp_ready)" == "1" ]; then
      break
    else
      local t_now=$(/bin/date +%s)
      local t_expected_min=$(($t_now + $snooze_secs - $detect_secs))
      local t_expected_max=$(($t_now + $snooze_secs + $detect_secs))
      /bin/sleep $snooze_secs
      local t_now=$(/bin/date +%s)
      if [ $t_now -le $t_busybox ]; then
        continue
      elif [ $t_now -lt $t_expected_min ] || [ $t_now -gt $t_expected_max ]; then
        /usr/sbin/nvram set ntp_ready=1
        /usr/sbin/nvram set svc_ready=1
        break
      fi
    fi
  done
}

wait_for_clock_change



/jffs/scripts/init-start
Code:
#!/bin/sh
# disable the Asus NTP service and client
[ -n "$(/bin/pidof ntp)" ] && /usr/bin/killall ntp && /bin/sleep 1
/bin/rm -f /var/run/ntp.pid
/bin/mkdir -p /var/run/ntp.pid
[ -n "$(/bin/pidof ntpclient)" ] && /usr/bin/killall ntpclient && /bin/sleep 1
/bin/mount -o bind /dev/null /usr/sbin/ntpclient

# automatically set ntp_ready: runs in background and waits indefinitely for a clock change then sets ntp_ready and quits
/jffs/scripts/set_ntp_ready.sh &
 
Last edited:
I tried this other way, and it works too. It is more simple.

/jffs/scripts/set_ntp_ready.sh
Code:
#!/bin/sh
wait_for_clock_change() {
  local snooze_secs=2
  local detect_secs=10
  local t_busybox="$(/bin/date -r /bin/busybox +%s)"

  while : ; do
    if [ "$(/usr/sbin/nvram get ntp_ready)" == "1" ]; then
      break
    else
      local t_now=$(/bin/date +%s)
      local t_expected_min=$(($t_now + $snooze_secs - $detect_secs))
      local t_expected_max=$(($t_now + $snooze_secs + $detect_secs))
      /bin/sleep $snooze_secs
      local t_now=$(/bin/date +%s)
      if [ $t_now -le $t_busybox ]; then
        continue
      elif [ $t_now -lt $t_expected_min ] || [ $t_now -gt $t_expected_max ]; then
        /usr/sbin/nvram set ntp_ready=1
        /usr/sbin/nvram set svc_ready=1
        break
      fi
    fi
  done
}

wait_for_clock_change



/jffs/scripts/init-start
Code:
#!/bin/sh
# disable the Asus NTP service and client
/usr/bin/killall -q ntp
/bin/rm -rf /var/run/ntp.pid
/bin/mkdir -p /var/run/ntp.pid
/usr/bin/killall -q ntpclient
/bin/mount -o bind /dev/null /usr/sbin/ntpclient

# automatically set ntp_ready: runs in background and waits indefinitely for a clock change then sets ntp_ready and quits
/jffs/scripts/set_ntp_ready.sh &
Thanks, just rebooted after changing to the above and no warning :)
 
Well so far so good, past the 24 hour mark and things are very steady now, sub ms readings :)

If only I could get the bloody thing working with my GPS :mad:
 
Well so far so good, past the 24 hour mark and things are very steady now, ...
You don't need to be afraid of this coming back and interfering with the clock. Normally, the command service start_ntpc, would start both ntp and ntpclient. That's how I tested it.
 
There are some significant changes made to the webui with firmware 380.68.

Backported the menuTree API from GPL 382. This new module takes care of generating the menu and tabs using objects rather than a messy set of arrays with exception handling. There should be no real visible difference to end-users, however developers who modify the webui will need to adjust to the new system (which should make it much easier now to add your own tabs to the existing menus).

This is resulting in the NTP Stats page not being listed under Tools. The only way to access it is to manually enter <router IP address>/Tools_NtpdStats.asp in the address bar.

I believe the problem lies in the state.js file. The strings that are being searched for with sed are not in the state.js file anymore. As a result the NTP Daemon tab is not added to the Tools page.

  • sed -i 's/Other Settings");/Other Settings", "NTP Daemon");/' /www/state.js
  • sed -i 's/therSettings.asp");/therSettings.asp", "Tools_NtpdStats.asp");/' /www/state.js

These do not work now.
 
There are some significant changes made to the webui with firmware 380.68.
Just a little request, when you update to support the new webui, please don't wipe out the old webui support (I don't have any plans to update my LTS fork). Thanks for at least considering it.
 
There are some significant changes made to the webui with firmware 380.68.
This is resulting in the NTP Stats page not being listed under Tools. The only way to access it is to manually enter <router IP address>/Tools_NtpdStats.asp in the address bar.

I believe the problem lies in the state.js file. The strings that are being searched for with sed are not in the state.js file anymore. As a result the NTP Daemon tab is not added to the Tools page.

These do not work now.

https://www.snbforums.com/threads/alpha-asuswrt-rmerlin-380-68-pre-release.40480/page-3#post-340904

2017-08-20_09-34-46.png
 
Last edited:
@Martineau ...
Your tool works just fine for me, it comes after every firmware update to use. Only, I must then again install the NTP daemon.
 
@Martineau, thanks for keeping it updated. Where would I be without my graphs :confused:

One question though: if I do a fresh install, is Step 5 in @kvic's tutorial, the only thing I have to skip? Or are there any other sections/commands I can skip?

Maybe a bit neurotic but I just don't like the idea of performing unnecessary modifications, even if they don't have any effect or do harm now, because if I change something that causes trouble in the future, I can't trace it back due to memory issues.

Thanks in advance :)
 
Last edited by a moderator:
@Martineau, thanks for keeping it updated. Where would I be without my graphs :confused:

One question though: if I do a fresh install, is Step 5 in @kvic's tutorial, the only thing I have to skip? Or are there any other sections/commands I can skip?

Thanks in advance :)

For users that do not wish to use my RefreshWWW.sh script, @kvic's instructions Wiki probably should be updated as follows:

Step 5:
Patch files for WebUI and restart httpd
(for firmware 380.67 or earlier)
Code:
cp /opt/var/spool/ntp/Tools_NtpdStats.asp /www

sed -i 's/Other Settings");/Other Settings", "NTP Daemon");/' /www/state.js
sed -i 's/therSettings.asp");/therSettings.asp", "Tools_NtpdStats.asp");/' /www/state.js

service restart_httpd

Patch files for WebUI and restart httpd (for firmware 380.68 or later)
Code:
cp /opt/var/spool/ntp/Tools_NtpdStats.asp /www

sed -i '/"Tools_OtherSettings.asp", tabName: "Other Settings"/a {url: "Tools_NtpdStats.asp", tabName: "NTP Daemon"},' /www/require/modules/menuTree.js

service restart_httpd

but there are no other modifications required.
 
Last edited:
For users that do not wish to use my RefreshWWW.sh script, @kvic's instructions Wiki probably should be updated as follows:

Step 5:
Patch files for WebUI and restart httpd
(for firmware 380.67 or earlier)
Code:
cp /opt/var/spool/ntp/Tools_NtpdStats.asp /www

sed -i 's/Other Settings");/Other Settings", "NTP Daemon");/' /www/state.js
sed -i 's/therSettings.asp");/therSettings.asp", "Tools_NtpdStats.asp");/' /www/state.js

service restart_httpd

Patch files for WebUI and restart httpd (for firmware 380.68 or later)
Code:
cp /opt/var/spool/ntp/Tools_NtpdStats.asp /www

sed -i '/"Tools_OtherSettings.asp", tabName: "Other Settings"/a {url: "Tools_NtpdStats.asp", tabName: "NTP Daemon"},' /www/require/modules/menuTree.js

service restart_httpd

but there are no other modifications required.

I was just about to give that same information! Thanks Martineau!
 
Help! I need help in troubleshooting this. I have an RT-AC3200 with 380.68_2 FW installed and it worked great for a week but now the graphs are just blank, there's no logging going on.

rrdtool is installed and up to date

Update: scratch the above , I found my answer in posts 206 and 207
 
Last edited:
Just installed on AC88U running 382.1 and so far so good (albeit only 20 mins in)! No special considerations on install. Great tweak!!

I'll update you if anything goes awry!
 

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