gpt4 book ai didi

linux - 获取脚本的 ntpdate 结果

转载 作者:太空狗 更新时间:2023-10-29 11:15:53 27 4
gpt4 key购买 nike

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

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

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

NTP 同步有效,但当我发出“echo $RESULT”时,它的值始终为 0。所以我的问题是:有没有正确的方法来做到这一点?怎么办?

最佳答案

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

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"

关于linux - 获取脚本的 ntpdate 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9326336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com