4

I have a Python script that runs at startup and needs the correct time in order to function properly. I have setup the raspi-config to wait for a network connection before continuing so the code won't run until there is an internet connection (I think!). However, I have noticed that after a reboot it may take several seconds after the Python code gets started before the system gets around to running the NTP process that updates the system clock.

Is there some way to test to see when the internal clock gets updated by the NTP process? Is there some resource that keeps track of the last NTP time sync event? And how often does the RPi resync its clock using the NTP (daily, hourly weekly)?

2
  • Have you considered adding a RTC to the Pi? How precise a time do you need? What is the use case? Commented Aug 7, 2017 at 19:25
  • I wanted to avoid adding any additional hardware. The application is a kid's alarm clock (with the added bonus of Mom and Dad being able to set and turn off the alarm via iPhones). Commented Aug 9, 2017 at 0:05

1 Answer 1

2
  1. ntpstat Show network time synchronisation status

pi@Pi3-01:~ $ ntpstat synchronised to NTP server (193.228.143.24) at stratum 3 time correct to within 84 ms polling server every 1024 s

And with pythons ntplib you can get the NTP status.

  1. The poll time (in seconds) is shown in the column poll when you type the command ntpq -p

pi@Pi3-01:~ $ ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *ntp8.flashdance 193.11.166.20 2 u 162 1024 373 28.540 0.994 4.545 +195.78.244.34 62.149.0.30 2 u 106 1024 375 87.132 -1.339 5.561 +tethys.hot-chil 193.67.79.202 2 u 759 1024 377 58.820 -1.213 3.116 +sundown.speedwe 87.242.168.84 2 u 59 1024 377 59.788 0.605 3.521

  1. How often does it sync the time?

Initially every 64th second, if the time is stable, the poll time will increase, and vice versa if it differ to much.

From: http://doc.ntp.org/4.1.0/ntpd.htm

How NTP Operates

The ntpd program operates by exchanging messages with one or more configured servers at designated poll intervals. When started, whether for the first or subsequent times, the program requires several exahanges from the majority of these servers so the signal processing and mitigation algorithms can accumulate and groom the data and set the clock. In order to protect the network from bursts, the initial poll interval for each server is delayed an interval randomized over 0-16s. At the default initial poll interval of 64s, several minutes can elapse before the clock is set. The initial delay to set the clock can be reduced using the iburst keyword with the server configuration command, as described on the Configuration Options page.

6
  • When I type in ntpstat I get this: ntpstat: command not found I am running Jessie. Commented Aug 9, 2017 at 0:08
  • Install it with: sudo apt-get install ntpstat
    – MatsK
    Commented Aug 9, 2017 at 0:16
  • I got it installed OK. Here is the output: synchronised to NTP server (192.111.144.114) at stratum 3 time correct to within 124 ms polling server every 1024 s So how do I determine that this output is current and not leftover from a previous NTP time sync event that occurred before the reboot event? Commented Aug 9, 2017 at 20:27
  • The second command shows?
    – MatsK
    Commented Aug 9, 2017 at 21:47
  • The ntpq -p gets me this: ` remote refid st t when poll reach delay offset jitter ============================================================================== *ntp.your.org .CDMA. 1 u 795 1024 377 39.404 1.084 19.478 +clocka.ntpjs.or 35.73.197.144 2 u 376 1024 377 39.151 7.953 4.993 +li290-38.member 128.138.141.172 2 u 664 1024 177 61.149 4.258 14.380 +209.208.79.69 130.207.244.240 2 u 407 1024 377 47.402 0.707 24.355` Commented Aug 17, 2017 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.