2014-10-07 44 views
1

我有一個bash腳本,它通過SSH在遠程計算機上運行許多任務。每個腳本都很長,所以我想同時運行它們作爲後臺任務。在繼續之前,我還需要完成所有工作。我知道我可以對後者使用wait命令,但是當我在末尾粘貼&以使其成爲後臺任務時,它將停止工作。如何在bash中觸發多個遠程shell腳本併發運行,然後等待它們完成?

停止工作,我的意思是腳本似乎沒有運行,但否則主腳本仍然完成。

ssh [email protected] 'bash -s' < script1 my_parameter 
ssh [email protected] 'bash -s' < script2 my_parameter 
ssh [email protected] 'bash -s' < script3 my_parameter 

wait 

some_other_task 

回答

1

這工作得很好,我與Ubuntu 11.04:command.sh的

#!/bin/bash 
ssh [email protected] <command.sh & 
ssh [email protected] <command.sh & 
ssh [email protected] <command.sh & 

wait 

echo end 

內容:

sleep 5 
hostname 


更新:

用命令行參數它得到莫複雜。您可以使用「here-document」和set來設置$ 1 $ 2 $ 3。

#!/bin/bash 

ssh -T [email protected] << EOF 
#!/bin/bash 
set -- $my_parameter1 $my_parameter2 $my_parameter3 
$(cat script1) 
EOF 

ssh -T [email protected] << EOF 
#!/bin/bash 
set -- $my_parameter1 $my_parameter2 $my_parameter3 
$(cat script2) 
EOF 

ssh -T [email protected] << EOF 
#!/bin/bash 
set -- $my_parameter1 $my_parameter2 $my_parameter3 
$(cat script3) 
EOF 

wait 

echo end 
+0

我試過這個,但失敗了,因爲我傳遞了參數給腳本。我沒有提到這個問題。我會更新我的示例代碼。錯誤是'bash:my_parameter:command not found' – Simmo 2014-10-07 10:55:33

+0

我已經更新了我的答案。 – Cyrus 2014-10-07 15:55:15

相關問題