2012-12-11 55 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

啊......我明白你的意思了......那个工作谢谢......那么我需要做什么才能看到所有的流程都已经完成了? –

相关问题