Good day,
I am trying to add signal handling capabilities to some of my scripts. Unfortunately, I am having some difficulty with the manner in which signals are propagated between parent/child processes. Consider the following example:
I have the following "parent" script:
#!/usr/bin/sh
child_id=0
SIGINT_handler()
{
echo "SIGINT caught in Parent"
if [ $child_id -ne 0 ]
then
echo Parent sendingi: kill -n 2 $child_id
kill -n 2 $child_id
fi
}
echo parent running
trap 'echo parent exiting; exit' 0
trap 'SIGINT_handler' 2 # Pass Signal 2 to child but don't die
./child &
child_id=$!
echo 'child_id = ' $child_id
# Wait until child-process exits
wait $child_id
WAIT_STATUS=$?
echo Wait Status recorded when parent continues: $WAIT_STATUS
echo Parent Still Running after the child exits!
sleep 1000
which calls the following "child" script (as a background process):
#!/usr/bin/sh
echo child started. pid is $$
#trap 'echo child exiting; exit 0' 0
trap 'echo child got signal 2; exit 0' 2
sleep 1000
When I execute the "parent" process with a ">./parent",
I observe that the expected behavior. I then send a
SIGINT to the "parent" via a ">kill -s SIGINT <ppid>" and
get the following screen output:
When I look at the processes that are still active, I see
that the child (and its sleep) has not been killed at all -
all that happened was that the sleep-process of the parent
was activated.
I would appreciate it if anybody has some idea what is happening
here.