进程退出时未传递 SIGCHLD

mar*_*trz 3 bash bash-trap

我需要检测我的后台进程之一何时退出。因此,我安装了一个陷阱。run_gui并且run_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
Run Code Online (Sandbox Code Playgroud)

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

为什么我想念SIGCHLD

Cyr*_*rus 5

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

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