Trapping CHLD signal - ZSH works but ksh/bash/sh don't?

泄露秘密 提交于 2019-12-07 11:13:00

问题


Here's a sample code where a shell script launches a few jobs in the background and upon receiving the CHLD signal (i.e. the child process termination) it will take some actions... The problem is that if the parent shell script is a ZSH one, it works just fine and traps the CHLD signals, but other shells do not! why is that?

#! /bin/zsh -

function foo() { echo "Trapped CHLD signal!" 
}   

trap 'foo' CHLD

./child-work1.sh &
./child-work2.sh &
./child-work3.sh &

echo 'waiting for the children'
wait
echo '--------- done ---------'

回答1:


Bash automatically enables job control when interactive, but in scripts you must turn it on explicitly.

set -m


来源:https://stackoverflow.com/questions/9560245/trapping-chld-signal-zsh-works-but-ksh-bash-sh-dont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!