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

1

4

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 ---------'
Thespian answered 5/3, 2012 at 0:28 Comment(1)
is the exit code/signal the first argument to foo()? do you mean "exit code" instead of "signal"?Hadlee
A
12

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

set -m
Atticism answered 14/3, 2012 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.