2012-02-17 35 views
3

我有两个具有不同IP的NTP服务器。获取用于脚本的ntpdate结果

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

我试图创建一个名为导致这样一个系统变量: RESULT =用ntpdate 192.168.100.41

NTP同步工作,但是当我做“回声$结果”,其值始终为0 所以我的问题是: 有没有一个正确的方法来做到这一点?怎么样?

回答

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 

另外,IR你是Debian的,使用用ntpdate-debian的的/ etc /默认/用ntpdate指定服务器列表,例如:

NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org" 
+2

或者,更简洁地说,'ntpdate server1 || ntpdate server2'。不是'ntpdate'本身能够回退到不同的服务器吗?只需将其配置为使用您的两台服务器。 – tripleee 2012-02-17 10:47:15

+0

哇“echo $?” jejeje。非常好,谢谢barti_ddu – 2012-02-17 10:58:27