On Tue, 2 May 2006, Hans-Werner Hilse wrote:

Based on the suggestions of Uwe and Vladimir, I tried
        trap 'pkill -TERM -P $$; kill -s TERM $$' TERM
        <do something>
        . /path/to/child.sh
        <do something else>
Doesn't work, yet. Note that child.sh is a shell script that may execute
some other command (like rsync), so the "." by itself may not be enough.

This can't work because of this (man bash):
--snip
If bash is waiting for a command to complete and receives a signal for
which a trap has been set, the trap will  not  be  executed until the
command completes.
--snip

Oops...!
What instead works (just tested):
--snip
#!/bin/sh
COMMAND="sleep 120"

# First we background:
$COMMAND &
# Save the PID
CHILDPID=$!
# Trap the signal:
trap "kill -TERM $CHILDPID" TERM
# And wait for the Child to finish:
wait $CHILDPID
# reset signal handling:
trap - TERM
--snip

Backgrounding is really not an option. The whole setup is to be supervised
by daemontools.
Note that the code could hit a racing condition and should therefore
not carelessly run by root on a machine with untrusted users. This is:
The process may have finished before setting the signal handler.
Other processes *might* reuse the PID afterwards and might get
sig-TERM-ed until resetting the signal handler again. Probably a minor,
depending on the script's usage.

Precisely the kind of thing I want to avoid. I think I need to
reformulate my setup.

Thanks.

Jorge Almeida
--
gentoo-user@gentoo.org mailing list

Reply via email to