User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

NTP not syncing with script.

Sun Sep 20, 2015 8:38 am

Hi,

I have added a DS3231 hardware clock to my pi. I am trying to sync every morning with the following script....But it always returns 'Timesync not done, exiting'.

Code: Select all

#!/bin/sh
/usr/sbin/ntp-wait -n 5 -s 3
RETVAL=$?

if [ "$RETVAL" != "0" ];then
echo "Timesync not done, exiting "
exit 1
fi
When testing (from what I found on the net) with ntpq -p it always gives...
ntpq: read: Connection refused

Can anyone tell me where I have gone wrong? I just want to update one pi every day to act as a time server to the rest of the network (reducing external network load).

Thanks if you can help.....

User avatar
DougieLawson
Posts: 38815
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: NTP not syncing with script.

Sun Sep 20, 2015 9:23 am

ntpq needs ntpd running to avoid getting the connection refused error.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Mon Sep 21, 2015 8:44 am

I'll give it a look today.

Thank you for your answer!

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Mon Sep 21, 2015 10:46 am

OK so that works, trouble is that ntpd is not started at boot.

I tried putting it in rc.local and that didn't work.
I tried update-rc.d and got the following error.....

update-rc.d: error: unable to read /etc/init.d/ntpd

Then I tried a bash script called from cron @reboot and that wouldn't work either!

What is the 'correct' way to start ntpd at boot/reboot?

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: NTP not syncing with script.

Mon Sep 21, 2015 11:31 am

ntpd should already start at boot. If it doesn't sync it may be that it is being started before your internet connection is ready.

As to how to get it set the start at boot, you were close with your update-rc.d, but the service name is ntp, not ntpd.

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Mon Sep 21, 2015 12:24 pm

Aha! - They don't make it easy do they? :roll:

Now I get a new problem.....

Code: Select all

pi@raspberrypi2 ~ $ sudo update-rc.d ntp start
update-rc.d: using dependency based boot sequencing
Use of uninitialized value in string eq at /usr/sbin/update-rc.d line 490.
update-rc.d: warning:  start runlevel arguments (none) do not match ntp Default-Start values (2 3 4 5)
insserv: warning: current start runlevel(s) (empty) of script `ntp' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (2 3 4 5) of script `ntp' overrides LSB defaults (empty).
Any help with this gratefully received! :?

User avatar
DougieLawson
Posts: 38815
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: NTP not syncing with script.

Mon Sep 21, 2015 7:50 pm

Try
sudo apt-get purge ntp
sudo apt-get install ntp

That should cleanup and re-install the auto start script.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Mon Sep 21, 2015 9:35 pm

Thanks Dougie, that seems to have sorted the issue. ntpq -p now works after a reboot from the CLI.

I guess ntp was corrupted somehow?

I will test the network NTP service to other Rpis this week (I hope)....

Thank you!

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Tue Sep 22, 2015 1:18 pm

Code: Select all

#!/bin/sh
/usr/sbin/ntp-wait -n 5 -s 3
RETVAL=$?

if [ "$RETVAL" != "0" ];then
echo "Timesync not done, exiting "
exit 1
fi
This still fails to sync. ntp is running. ntpq -p works. but this script (called from cron @reboot) always returns the fail message. Even running the script from the cli fails to sync.

Any ideas please?

For clarity, I want the pi on reboot or at a predefined time (cron job) to update the hwclock (DS3231) and ensure the ntp time, hwclock time and pi time are all in sync and correct. This pi will then be the time server to other pi and arduinos on the network.

User avatar
skywatch
Posts: 46
Joined: Tue May 21, 2013 8:17 pm

Re: NTP not syncing with script.

Sun Sep 27, 2015 10:06 am

Oddly, if left overnight and called from cronjob it syncs. Running from a @reboot cron (with or without a 20 minute delay) and it always fails. I am stumped by this behaviour!

Return to “Troubleshooting”