For Script in Script Shell

时间:2012-10-10 19:26:22

标签: shell

我的脚本是:

for i in $(seq $nb_lignes) a list of machines
do
ssh root@$machine -x "java ....." 
sleep 10
done

- >我从机器C

执行此脚本

我有两台机器A和B($ nb_lignes = 2)

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

我从机器C运行脚本: 我希望它显示:创建节点1,等待10秒,然后创建显示节点2

我的问题:显示节点1仅创建

我用磁带ctrl + c创建了diplay节点2

PS:两个进程java仍在机器A和B中运行

谢谢

2 个答案:

答案 0 :(得分:2)

从我读这篇文章的方式来说, armani 是正确的;因为你的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 root@${host} "java ....." 
    sleep 10
done

答案 1 :(得分:1)

试试“&” “ssh”命令后的字符。这会单独产生[背景]进程并继续使用脚本。

否则,您的脚本将无法运行ssh。

编辑:为清楚起见,这将是您的脚本:

for i in $(seq $nb_lignes) a list of machines
do
ssh root@$machine -x "java ....." &
sleep 10
done