3

我有两个具有不同 IP 的 NTP 服务器。

我正在尝试制作一个将系统时钟与“ntpdate”客户端同步的bash脚本。如果第一个 NTP 服务器没有响应,我的脚本应该尝试连接到第二个 NTP 服务器。

我试图以这种方式创建一个名为 RESULT 的系统变量:RESULT = ntpdate 192.168.100.41

NTP 同步有效,但是当我制作“echo $RESULT”时,它的值始终为 0。所以我的问题是:有正确的方法吗?如何?

4

1 回答 1

2

只需检查存储在中的执行状态$?

ntpdate foo.com
17 Feb 12:35:13 ntpdate[16218]: no server suitable for synchronization found
echo $?
1

ntpdate 1.europe.pool.ntp.org
17 Feb 12:36:26 ntpdate[16220]: step time server 109.230.243.8 offset 27.014301 sec
echo $?
0

或者,如果您使用的是 Debian,请使用ntpdate-debian并在/etc/default/ntpdate中指定服务器列表,例如:

NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org"
于 2012-02-17T10:38:44.453 回答