cmkelley
Very Senior Member
If you have a local time server on your network, below is a script that can be called from init-start to set your router clock as early as possible in the bootup sequence. Put it in /jffs/scripts and of course make it executable with chmod a+x fetch-ntp
To use, add the following line to your init-start script:
Where IP.OF.YOUR.SERVER is the IP of your local NTP sever and "count" is the number of cycles (1 second each) you want it to try before giving up. The '&' at the end is important to spawn it as a separate task. The timelog.startup file is interesting about once or twice to verify it's all working nicely, and then you can delete the relevant lines.
With my system it syncs to my time server on the second try, so 2 seconds after the init-start script runs. On my system, that seems to be about 2-3 seconds before the next script in line, services-start runs, and 4-5 seconds before dnsmasq and wan-start run.
My services-start and wan-start scripts actually don't do anything right now except write to logger; leftovers from figuring out the order that scripts run.
Code:
#!/bin/sh
#
# Script: fetch-ntp
#
# script to get time from time server as quickly as possible
# call from init-start as "/jffs/scripts/fetch-ntp ww.xx.yy.zz cc &"
# where ww.xx.yy.zz is IP address (duh!) and cc is number of cycles to loop
# do not use URL; use IP only so we don't wait for DNS to come up
# `&` at end is critical - spawns this as a separate task and continues
# verify 2 parameters passed, no other error checking
if [ "X$2" = "X" ]; then exit; fi
ntp_ip=$1
cycles=$2
count=$cycles
rm /jffs/timelog.startup
touch /jffs/timelog.startup
while [ $count -gt 0 ]
do
echo "Cycle #$count:" $(date) >> /jffs/timelog.startup
sleep 1 # give it a chance to wake up
count=`expr $count - 1`
if ping -w 1 -c 1 $ntp_ip > /dev/null 2>&1 # Give me a ping, Vasily. One ping only, please.
then
if ntpd -q -n -p $ntp_ip
then
logger -t "SCRIPT_$(basename $0)" "Time synced from $ntp_ip in `expr $cycles - $count` cycles"
exit
else
logger -t "SCRIPT_$(basename $0)" "FAILED to sync time from $ntp_ip! (valid ip)"
exit
fi
fi
done
logger -t "SCRIPT_$(basename $0)" "FAILED to find time server $ntp_ip in $cycles cycles!"
Code:
/jffs/scripts/fetch-ntp IP.OF.YOUR.SERVER count &
With my system it syncs to my time server on the second try, so 2 seconds after the init-start script runs. On my system, that seems to be about 2-3 seconds before the next script in line, services-start runs, and 4-5 seconds before dnsmasq and wan-start run.
Code:
May 4 22:05:06 kernel: wfd_registerdevice Successfully registered dev wl0.1 ifidx 1 wfd_idx 0
May 4 22:05:06 kernel: Register interface [wl0.1] MAC: [redacted]
May 4 22:05:06 kernel: wfd_registerdevice Successfully registered dev wl1.1 ifidx 1 wfd_idx 1
May 4 22:05:06 kernel: Register interface [wl1.1] MAC: [redacted]
Jan 11 10:55:29 SCRIPT_fetch-ntp: Time synced from [redacted] in 2 cycles
Jan 11 10:55:30 avahi-daemon[826]: WARNING: No NSS support for mDNS detected, consider installing nss-mdns!
Jan 11 10:55:31 lldpd[904]: cannot get ethtool link information with GLINKSETTINGS (requires 4.9+): Operation not permitted
Jan 11 10:55:31 avahi-daemon[826]: Alias name "RT-AC86U" successfully established.
Jan 11 10:55:32 SCRIPT_services-start: started []
Jan 11 10:55:34 SCRIPT_wan-start: started [0]
Jan 11 10:55:34 dnsmasq[814]: ignoring nameserver [redacted] - local interface
Jan 11 10:55:35 kernel: ubi1: attaching mtd9
Code:
#!/bin/sh
logger -t "SCRIPT_$(basename $0)" "started [$@]"