杀死子进程和陷阱

杀死子进程和陷阱

我有以下两个脚本来模拟一些工作:

start.sh只需使用 script 启动 2 (mpi) 个进程mpiproc.sh

启动文件

#!/bin/bash

function trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}

function handleSignal() {
    echo "Received signal (sleep for 10 sec)"
    for i in {1..2}
    do
      echo "start.sh: sleeping $i"
      sleep 1s
    done
    exit 0
}

# Setup the Trap
trap_with_arg handleSignal SIGINT SIGTERM SIGUSR1 SIGUSR2

mpirun -n 2 mpiproc.sh

mpiproc.sh

function trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}


function handleSignal() {
    echo "Rank: ${OMPI_COMM_WORLD_RANK} : Received signal (sleep for 10 sec)"
    for i in {1..10}
    do
      echo "Rank: ${OMPI_COMM_WORLD_RANK} sleeping $i"
      sleep 1s
    done
    exit 0
}

# Setup the Trap
trap_with_arg handleSignal SIGINT SIGTERM SIGUSR1 SIGUSR2

echo "MPI Proc  Rank: ${OMPI_COMM_WORLD_RANK} start."
sleep 30s

我运行脚本的集群start.sh向 start.sh 发送 SIGUSR2 信号(这就是我的想法)。问题是我的handleSignalin mpiproc 没有完成,因为 start.sh 已经执行了它的handleSignal并调用了exit 0。如何使handleSignal 调用沿着进程树向上移动?这意味着首先 mpiproc.sh 需要处理信号(start.sh 以某种方式等待该信号?),然后 start.sh 进行清理然后退出?

谢谢!

相关内容