1

If have a raspberry pi on Raspbian running a home automation server. That server depends upon accurate system time on the raspberry pi. When I have a power outage for a bit of time and then the power is restored, my raspberry pi restarts just fine, but it does not re-establish the proper time. It is configured to use external NTP servers to re-establish its time, but it does not do so properly in the specific case of a power outage. If I just unplug the raspberry pi and then plug it in again to restart it or if I power it down and then restart, it does establish proper system time from the external NTP servers.

My best guess (though this is just a guess) is that when power is restored after a power outage, internet connectivity is not immediately available when the Pi boots, though internet connectivity is just fine minutes later.

How can I make sure my raspberry Pi has the correct system time as soon as power and internet are restored? Note, this problem of inaccurate system time long after internet connectivity has been restored is not a short duration problem. I've seen it last for multiple days before I had to manually reboot the Raspberry Pi to fix it. As this is meant to be an unattended home automation server, I'm trying to fix the need for a manual reboot after power outage.

FYI, I'm not interested in adding a battery powered clock to the Raspberry Pi because I don't want this server to require battery maintenance (it's not in an easily located place and should run completely unattended). I just want it to pull the correct time from an external NTP server whenever internet service is restored. The internet connectivity for the Pi comes from hard-wired ethernet, not WiFi.


Edit: After reading more about ntpd, I'm not convinced that it actually does what I want if there is no network connectivity yet when it is started. Under some conditions, it seems like it may just exit if it finds the time too far off and in other circumstances, it may try to slowly drift the time to correct it which, after a 4 hour power outage, could take days to correct. Neither of those behaviors is desirable. I want a full time correction as soon as it first finds network connectivity and I want it to keep trying to reach its NTP server until the network is established and it can successfully reach it. Not sure whether it does all that. Maybe need an alternative?


FYI, my Raspberry Pi appears to be configured as such (default configuration for Raspbian):

/usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 102:104

-g configures it to accept any time offset upon first contact with the ntp server

-u specifies a user:group to switch to

-p writes the ntpd process to a specific file (so other apps can find it)
  • Possibly related: NTPd is not updating time though I don't think I have the firewall issue mentioned there because my issue only occurs when the PI boots while network connectivity is down. When network connectivity is up at boot time, ntpd works just fine so it seems unlikely my issue is a firewall issue. But, it does mention openntpd as a possible alternative. – jfriend00 Oct 23 '17 at 5:45
  • This Q&A shows what happens during power-outs: raspberrypi.stackexchange.com/questions/1397/… - NTP takes a while to synchronize. – SDsolar Jun 20 '18 at 1:34
1

Frankly, I would use a RTC. If powered normally the battery will last years, because it only uses battery power on standby (I have never had to replace mine). There are also RTC with supercapacitors.

timedatectl status will show if NTP is synchronised.

The ntpd daemon should adjust the time within 10 minutes if there has been significant drift.

  • Sorry, but this doesn't answer the question that was asked. The ntpd daemon is running, but is not correcting the time and I've already said that a battery powered RTC is simply not practical in this case (the server is mounted in a hard-to-reach place and needs to run for 10-20 years without maintenance). – jfriend00 Oct 23 '17 at 5:19
  • A supercapacitor RTC might be a possibility, but I can't find any information on how long the capacitor will run without power and how long it will last (in years) before it deteriorates since that still seems to be the weak link in electronics these days. I was hoping that there's just a problem with the ntpd deamon or configuration and we could find someway to correct that rather than add another component to the PI. – jfriend00 Oct 23 '17 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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