2012-12-11 50 views
0

我已經設置了幾個EC2實例,它們都在主目錄中有一個腳本。我想在每個EC2實例上同時運行腳本,即不經過循環。在多臺機器上運行相同的腳本

我見過csshX的OSX的終端互動的使用率......但不知道命令行代碼是執行像

ssh [email protected] . test.sh 

命令,因爲運行的所有實例的test.sh腳本是什麼?

csshX [email protected] [email protected] [email protected] . test.sh 

不工作...

我想這樣做在命令行,因爲我想通過增加它變成一個shell腳本來自動完成這個過程。

以及獎勵積分...如果有一種方法可以發送消息回機器發送已完成運行腳本的命令,那將是太棒了。

+0

不要忘了請接受最適合你的答案:-) – BobS

回答

1

它是否足以擁有一個在後臺運行所有這些東西的主shell腳本?例如,

#!/bin/sh 
pidlist="ignorethis" 
for ip in ip1 ip2 
do 
    ssh [email protected]$ip . test.sh & 
    pidlist="$pidlist $!" # get the process number of the last forked process 
done 

# Now all processes are running on the remote machines, and we want to know 
# when they are done. 

# (EDIT) It's probably better to use the 'wait' shell built-in; that's 
# precisely what it seems to be for. 
while true 
do 
    sleep 1 
    alldead=true 
    for pid in $pidlist 
    do 
     if kill -0 $pid > /dev/null 2>&1 
     then 
      alldead=false 
      echo some processes alive 
      break 
     fi 
    done 
    if $alldead 
    then 
     break 
    fi 
done 

回聲全部完成。

它不會完全同時,但它應該並行啓動遠程腳本。

+0

這並沒有什麼幫助,因爲test.sh腳本需要很長時間才能運行,我認爲它不會啓動下一個ssh直到第一個完成... –

+0

確定嗎?這裏的關鍵是在ssh行末尾的&,在後臺運行ssh調用。 – BobS

+0

啊......我明白你的意思了......那個工作謝謝......那麼我需要做什麼才能看到所有的流程都已經完成了? –

相關問題