2012-02-17 34 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