在许多机器上运行相同的脚本

时间:2012-12-11 03:14:48

标签: ssh amazon-ec2 cluster-computing

我已经设置了几个EC2实例,它们都在主目录中有一个脚本。我想在每个EC2实例上同时运行脚本,即不经过循环。

我已经看到了OSX的csshX用于终端交互式使用......但是想知道命令行代码是什么来执行像

这样的命令
ssh user@ip.address . test.sh

在所有实例中运行test.sh脚本,因为......

csshX user@ip.address.1 user@ip.address.2 user@ip.address.3 . test.sh 

不起作用......

我想在命令行上执行此操作,因为我希望通过将其添加到shell脚本中来自动执行此过程。

以及奖励积分...如果有办法将信息发送回机器发送命令,表明它已经完成了运行非常棒的脚本。

1 个答案:

答案 0 :(得分:1)

是否有足够好的主shell脚本在后台运行所有这些东西?如,

#!/bin/sh
pidlist="ignorethis"
for ip in ip1 ip2
do
    ssh user@$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

回应所有完成。

它不会完全同时发生,但它应该并行启动远程脚本。

相关问题