Hi, On Tue, 2 May 2006 17:42:26 +0100 (WEST) Jorge Almeida <[EMAIL PROTECTED]> wrote:
> On Tue, 2 May 2006, Zac Slade wrote: > > > You can find the PID of the last backgrouned process using the bash variable > > $! > > > The child is not backgrounded! > > So something like: > > subprocess & > > $pid=$! > > > > Using trap along with maybe setting alarms should get you what you want. > > > 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 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 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. -hwh -- gentoo-user@gentoo.org mailing list