在其他主机上使用`ssh -x -n`杀死父级生成的所有进程

时间:2010-10-30 07:42:58

标签: ssh kill background-process

您好 名为G09的软件使用Linda并行工作。它将其并行子进程在其他节点(主机)上生成为

/usr/bin/ssh -x compute-0-127.local -n /usr/local/g09l/g09/linda-exe/l1002.exel ...other_opts...

但是,当主节点终止此进程时,其他节点上的相应子进程(即compute-0-127)不会死,而是继续在后台运行。现在,我手动转到具有这些孤立的Linda进程的每个节点,并使用kill将其终止。有没有办法杀死这样的子进程?

在杀死进程之前查看用于PSTREE的pastebin 1,在父进程被杀之后查看用于PSTREE的pastebin 2 pastebin1 - http://pastebin.com/yNXFR28V
pastebin2 - http:// pastebin.com/ApwXrueh
- 没有足够的声誉点超链接第二个pastebin,对不起!(
更新到Answer1
谢谢马丁的解释。我试过跟着

killme() { kill 0 ; } ; #Make calls to prepare for running G09 ; 
g09 < "$g09inp" > "$g09out" &
trap killme 'TERM'
wait

但是当Torque / Maui(处理作业执行)将作业(此脚本)作为qdel $jobid杀死时,G09作为ssh -x $host -n启动的进程仍然在后台运行。我在这做错了什么? (正常终止不是问题,因为G09本身会停止这些进程。)pstree之前的qdel

bash
|-461.norma.iitb. /opt/torque/mom_priv/jobs/461.norma.iitb.ac.in.SC
|   `-g09
|       `-l1002.exe 1048576000Pd-C-C-addn-H-MO6-fwd-opt.chk
|           `-cLindaLauncher/tmp/viaExecDataN6
|               |-l1002.exel 1048576000Pd-C-C-addn-H-MO6-fwd-opt.ch
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   |-{l1002.exel}
|               |   `-{l1002.exel}
|               |-ssh -x compute-0-149.local -n ...
|               |-ssh -x compute-0-147.local -n ...
|               |-ssh -x compute-0-146.local -n ...
|               |-{cLindaLauncher}
|               `-{cLindaLauncher}
`-pbs_demux

qdel之后仍显示

461.norma.iitb. /opt/torque/mom_priv/jobs/461.norma.iitb.ac.in.SC
`-ssh -x -n compute-0-149 rm\040-rf\040/state/partition1/trirag09/461

l1002.exel 1048576000Pd-C-C-addn-H-MO6-fwd-opt.ch
|-{l1002.exel}
|-{l1002.exel}
|-{l1002.exel}
|-{l1002.exel}
|-{l1002.exel}
|-{l1002.exel}
|-{l1002.exel}
`-{l1002.exel}

ssh -x compute-0-149.local -n /usr/local/g09l/g09/linda-exe/l1002.exel

ssh -x compute-0-147.local -n /usr/local/g09l/g09/linda-exe/l1002.exel

ssh -x compute-0-146.local -n /usr/local/g09l/g09/linda-exe/l1002.exel

我在这里做错了什么?是trap killme 'TERM'错了吗?

2 个答案:

答案 0 :(得分:1)

我会尝试以下方法:

  • 创建一个脚本/应用程序,它包装您正在启动的这个g09二进制文件,然后启动该包装器
  • 在脚本中
  • ,等待HUP信号到达(应该在ssh连接关闭时收到)
  • 在处理HUP信号时,向您的进程组发送一个信号(即PID 0),以杀死该组中的所有进程。

向流程组发送KILL信号非常简单:kill -9 0。试试这个:

#!/bin/sh
./b.sh 1 &
./b.sh 2 &
sleep 10
kill -9 0

其中b.sh是

#!/bin/sh
while /bin/true
do
  echo $1
  sleep 1
done

您可以拥有任意数量的子进程(直接或间接);他们都会得到信号 - 只要他们不从过程组中分离出来。

答案 1 :(得分:0)

我使用ssh -N(类似于ssh -n)遇到类似的问题,如果我在启动ssh调用的脚本中运行它,kill -9 0对我不起作用。我发现kill jobs -p确实终止了ssh进程,这不是很优雅,但我目前正在使用它。