[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Is there a signal that can be automatically propogated to su
From: |
Peng Yu |
Subject: |
[Help-bash] Is there a signal that can be automatically propogated to subprocesses? |
Date: |
Sat, 14 Sep 2013 11:43:55 -0500 |
Hi,
The following code demonstrate that when script1.sh receives the TERM
signal, it will be terminated. But script2.sh still runs until it
finishes.
~/linux/test/bash/man/builtin/kill/-s/TERM/nested$ cat.sh *.sh
==> main.sh <==
#!/usr/bin/env bash
./script1.sh &
pid=$!
#wait
sleep 1
kill -s TERM $pid
==> script1.sh <==
#!/usr/bin/env bash
echo script1.sh: $$
trap "echo script1.sh: on trap TERM >&2; trap - TERM; kill -s TERM $$" TERM
./script2.sh &
wait $!
exit_code=$?
echo script1.sh: $exit_code
exit $exit_code
==> script2.sh <==
#!/usr/bin/env bash
echo script2.sh: $$
trap "echo script2.sh: on trap TERM >&2; trap - TERM; kill -s TERM $$" TERM
sleep 3 &
wait $!
exit_code=$?
echo script2.sh: $exit_code
exit $exit_code
~/linux/test/bash/man/builtin/kill/-s/TERM/nested$ ./main.sh
script1.sh: 83507
script2.sh: 83509
script1.sh: on trap TERM
~/linux/test/bash/man/builtin/kill/-s/TERM/nested$ script2.sh: 0
To terminate the subprocesses, I have do some thing like the following manually.
~/linux/test/bash/man/builtin/kill/-s/TERM/nested2$ cat script1.sh
#!/usr/bin/env bash
echo script1.sh: $$
function send_TERM_to_subprocesses {
local pid
for pid in "address@hidden"
do
kill -s TERM "$pid"
done
}
trap "echo script1.sh: on trap TERM >&2; send_TERM_to_subprocesses;
trap - TERM; kill -s TERM $$" TERM
./script2.sh &
pid=$!
subprocesses=("address@hidden" "$pid")
wait $pid
exit_code=$?
echo script1.sh: $exit_code
exit $exit_code
Now the subprocesses can be terminated. But this solution is kind of
tedious as I have to manually keep track all the subprocesses. Is
there a simpler solution to this problem?
~/linux/test/bash/man/builtin/kill/-s/TERM/nested2$ ./main.sh
script1.sh: 83770
script2.sh: 83772
script1.sh: on trap TERM
script2.sh: on trap TERM
--
Regards,
Peng
- [Help-bash] Is there a signal that can be automatically propogated to subprocesses?,
Peng Yu <=