进程退出时不会传递SIGCHLD

时间:2016-05-31 18:07:28

标签: bash bash-trap

我需要检测我的某个后台进程何时退出。因此,我安装了一个陷阱。 run_guirun_ai1是简单的exec函数。

run_gui & gui_pid=$!
run_ai1 & ai1_pid=$!
trap 'echo foo' SIGCHLD
while true; do
    echo "Started the loop"
    while true; do
        read -u $ai1_outfd line || echo "Nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
    while true; do
        read -u $gui_outfd line || echo "nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
done

当我关闭GUI时,没有任何反应。仅当我按ctrl + c时才会执行echo foo命令。

为什么我会错过SIGCHLD

1 个答案:

答案 0 :(得分:3)

  

默认情况下,非交互式shell不启用作业控制。看看   将'set -o monitor'放在脚本的开头会产生   你想要的结果。

资料来源:Chet Ramey,GNU Bash的维护者