Search code examples
c++linuxsystem-callsntpntpd

How to check ntpd is still running and synced


I'm calling ntp_gettime() and it is performing as expected however if I kill ntpd I'm still getting the correct behaviour with my return value showing no issues. This suggests ntp_gettime() does not call through to ntpd, which is what I believe was happening.

I'm trying to check that ntpd is still running correctly and that it still has a valid connection. I now assume that ntpd updates the system based on the defined interval and the ntp_gettime() call is calling just the system.

My question is can ntp_gettime() be used to determine if ntpd is running and that the server connection is still valid and have I just made a mistake somewhere? If not, is there a way to do this?


Solution

  • The way I ended up doing it is by using a pipe to make a ntpstat call and processing the output. That is I check for unsynchronised, synchronised, and the absence of synchronised (after checking for unsynchronised) and act accordingly based on the results above.

    If there is a better way to get the same result, because I think this is messy, please let me know.