如何检查 ntpd 是否仍然 运行 并且已同步

How to check ntpd is still running and synced

我正在调用 ntp_gettime() 并且它按预期执行但是如果我杀死 ntpd 我仍然得到正确的行为,我的 return 值没有显示任何问题。这表明 ntp_gettime() 没有调用 ntpd,我认为这是正在发生的事情。

我正在尝试检查 ntpd 是否仍然 运行 正确并且它仍然具有有效连接。我现在假设 ntpd 根据定义的时间间隔更新系统,并且 ntp_gettime() 调用仅调用系统。

我的问题是 ntp_gettime() 是否可用于确定 ntpd 是否为 运行 以及服务器连接是否仍然有效,我只是在某处犯了错误吗? 如果没有,有没有办法做到这一点?

您问题的答案是 "no"。这不是 NTP 服务器的接口,不能用于确定 NTP 服务器的状态。这是完全不同的东西。 According to the glibc documentation:

The ntp_gettime and ntp_adjtime functions provide an interface to monitor and manipulate the system clock to maintain high accuracy time. For example, you can fine tune the speed of the clock or synchronize it with another time source.

A typical use of these functions is by a server implementing the Network Time Protocol to synchronize the clocks of multiple systems and high precision clocks.

换句话说,这就是 NTP 服务器本身用来与内核对话、处理业务的内容。

检查守护进程运行

ntpctl 是一个显示有关 运行 ntpd 守护进程信息的程序,下面的命令将为您提供有关守护进程正在与多少台服务器同步的信息,系统时钟的状态(如果是 [=16] =] 和时钟偏移量。有关详细信息,请参阅 ntpctl https://man.openbsd.org/ntpctl 的联机帮助页。 Openntpd 软件包为您提供了 ntpctl 程序。

ntpctl -s all

我最终采用的方法是使用管道进行 ntpstat 调用并处理输出。 那就是我检查不同步,同步和不存在同步(在检查不同步之后)并根据上面的结果采取相应的行动。

如果有更好的方法得到相同的结果,因为我觉得这很乱,请告诉我。