进程退出时未传递 SIGCHLD

SIGCHLD not delivered when a process exits

我需要检测我的后台进程之一何时退出。因此,我安装了一个陷阱。 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 时,没有任何反应。 echo foo 命令只有在我按下 ctrl+c 时才会执行。

为什么我错过了 SIGCHLD

Non-interactive shells don't enable job control by default. See if putting 'set -o monitor' at the beginning of the script produces the results you want.

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