Hello:
Am trying to understand why the method #2 works but method #1 does not.
For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd
background procs
.
Method #1:
#!/bin/sh
ctrl_c()
{
echo "** Trapped CTRL-C"
sudo kill -9 $TEST_PARALLEL_1 $TEST_PARALLEL_2; exit
}
trap "ctrl_c" INT
./TEST-PARALLEL-1.sh &
TEST_PARALLEL_1=$!
./TEST-PARALLEL-2.sh
TEST_PARALLEL_2=$!
echo $TEST_PARALLEL_1 $TEST_PARALLEL_2 )
/usr/bin/wait $TEST_PARALLEL_1 $TEST_PARALLEL_2
[aa229082@esmqst13:/home/aa229082 ] ps -ef | grep TEST
aa229082 123053 1 0 13:19 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL-1.sh
aa229082 123054 1 0 13:19 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL-2.sh
Method #2:
(trap 'kill 0' SIGINT
./TEST-PARALLEL-1.sh &
TEST_PARALLEL_1=$!
./TEST-PARALLEL-2.sh
TEST_PARALLEL_2=$!
echo $TEST_PARALLEL_1 $TEST_PARALLEL_2 )
[aa229082@esmqst13:/home/aa229082 ] ps -ef | grep TEST
aa229082 1439 120859 0 14:52 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL.sh
aa229082 1440 1439 0 14:52 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL.sh
aa229082 1441 1440 0 14:52 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL-1.sh
aa229082 1442 1440 0 14:52 pts/1 00:00:00 /bin/sh ./TEST-PARALLEL-2.sh
So for method #1, the spawned BG Procs have init as the PPID.
so even the "sudo kill -9" does not kill them
for method #2, we are using "kill 0", but this works.
But "kill 0" does not actually kill but :
"If sig is 0, then no signal is sent, but error checking is still performed."
So looks like Method #2 works solely because the whole thing is executed
from the Sub-Shell.
Curiously, the additional commands like "echo $TEST_PARALLEL_1 $TEST_PARALLEL_2"
did not show on the STDOUT.
Just trying to better understand the whole process.
Thnx