2012-10-10 61 views
1

我的腳本循環爲:對於在腳本殼牌

for i in $(seq $nb_lignes) a list of machines 
do 
ssh [email protected]$machine -x "java ....." 
sleep 10 
done 

- >我執行從設備C此腳本

我有兩臺機器A和B($ nb_lignes = 2)

ssh [email protected]$machineA -x "java ....." : create a node with Pastry overlay 
wait 10 secondes 
ssh [email protected]$machineB -x "java .....":create another node join the first (that's way i  have use sleep 10 secondes) 

i。從設備C運行腳本: 我想,它顯示:節點1被創建,等待10秒,並顯示節點2被創建

我的問題:它顯示節點1僅創建

我條C它diplay節點2被創建CTRL +

PS:兩個過程的java仍然在機器A和B

謝謝乳寧你

+2

你的循環是'因爲我在......',但你引用'$機'在循環體內。我想你的真正意思是'機器在...'中。請發佈儘可能接近您實際運行的代碼的代碼,以便我們不必猜測原始代碼中的哪個問題以及您重新輸入時引入的問題。 –

回答

1

嘗試「ssh」命令後的「&」字符。這個過程分別產生[背景]並繼續與腳本。

否則,您的腳本會卡住運行ssh。

編輯:爲清楚起見,這將是你的腳本:

for i in $(seq $nb_lignes) a list of machines 
do 
ssh [email protected]$machine -x "java ....." & 
sleep 10 
done 
+0

進一步解釋:被困在第一個SSH程序中,當你Ctrl-C你殺死THAT進程時,然後允許你移動到創建節點2.看看爲什麼在你點擊Ctrl-C之後會這麼說? – armani

+0

沒有與CTRL-C的進程沒有被殺死,當我點擊CTRL + C它通過au第二次迭代(shell不會返回與CTRL + C ..它通過au第二次迭代,它顯示節點2創建!! – user1735757

+0

:我現在無法訪問機器,我已經添加了&但它沒有執行睡眠10! – user1735757

2

從辦法,我讀這篇文章,阿瑪尼是正確的;因爲你的java程序沒有退出,所以循環的第二次迭代不會運行,直到你「破壞」第一次。我猜測Java程序忽略了ssh發送給它的中斷信號。

而不是背景每個SSH與&,你可能會更好使用由ssh本身提供給你的工具。從SSH手冊頁:

-f  Requests ssh to go to background just before command execution. 
     This is useful if ssh is going to ask for passwords or 
     passphrases, but the user wants it in the background. This 
     implies -n. The recommended way to start X11 programs at a 
     remote site is with something like ssh -f host xterm. 

所以......你的腳本會是這個樣子:

for host in machineA machineB; do 
    ssh -x -f [email protected]${host} "java ....." 
    sleep 10 
done 
+0

+1不錯的選擇... – Baba