From: kapul on
Hello!

I've got a question regarding signal sending and/or handing in child
processes of bash script.

The script simply executes itself as a subprocess:

$ cat script
#!/bin/bash
#set -x
set -e
set -u

if [ "$#" = 0 ]
then
# Run N tasks
N=3
child_pids=""
for i in $(seq 1 $N)
do
$0 child &
child_pids="$child_pids $!"
sleep 0.1
done
trap "set -x; kill -INT $child_pids; wait; exit 1" INT
wait
exit 0
fi

if [ "$1" = "child" ]
then
for n in $(seq 0 1000)
do

echo "* trying $n ..."
trap "set -x; echo \"** SIGINT: Deleting intermediate files ...
\";
exit 1" INT

echo "* Doing $n"

sleep $((5 + $RANDOM / 10000))
done
fi

When script is run with no arguments:

$ ./script

it executes N (N=3) subprocesses in the background.

Parent process as well as all subprocesses sets own handlers (using
trap) on INT signal. When INT signal
(^C or kill -INT PID) is sent to process it's not propagated to
subprocesses! Why??
What is interesting that even INT signal sent directly to subprocesses
(using kill) is ignored.

What's happening here, any ideas? Please help ;)

M.