1 |
On Tue, 2 May 2006, Hans-Werner Hilse wrote: |
2 |
|
3 |
>> Based on the suggestions of Uwe and Vladimir, I tried |
4 |
>> trap 'pkill -TERM -P $$; kill -s TERM $$' TERM |
5 |
>> <do something> |
6 |
>> . /path/to/child.sh |
7 |
>> <do something else> |
8 |
>> Doesn't work, yet. Note that child.sh is a shell script that may execute |
9 |
>> some other command (like rsync), so the "." by itself may not be enough. |
10 |
> |
11 |
> This can't work because of this (man bash): |
12 |
> --snip |
13 |
> If bash is waiting for a command to complete and receives a signal for |
14 |
> which a trap has been set, the trap will not be executed until the |
15 |
> command completes. |
16 |
> --snip |
17 |
> |
18 |
Oops...! |
19 |
> What instead works (just tested): |
20 |
> --snip |
21 |
> #!/bin/sh |
22 |
> COMMAND="sleep 120" |
23 |
> |
24 |
> # First we background: |
25 |
> $COMMAND & |
26 |
> # Save the PID |
27 |
> CHILDPID=$! |
28 |
> # Trap the signal: |
29 |
> trap "kill -TERM $CHILDPID" TERM |
30 |
> # And wait for the Child to finish: |
31 |
> wait $CHILDPID |
32 |
> # reset signal handling: |
33 |
> trap - TERM |
34 |
> --snip |
35 |
> |
36 |
Backgrounding is really not an option. The whole setup is to be supervised |
37 |
by daemontools. |
38 |
> Note that the code could hit a racing condition and should therefore |
39 |
> not carelessly run by root on a machine with untrusted users. This is: |
40 |
> The process may have finished before setting the signal handler. |
41 |
> Other processes *might* reuse the PID afterwards and might get |
42 |
> sig-TERM-ed until resetting the signal handler again. Probably a minor, |
43 |
> depending on the script's usage. |
44 |
> |
45 |
Precisely the kind of thing I want to avoid. I think I need to |
46 |
reformulate my setup. |
47 |
> |
48 |
Thanks. |
49 |
|
50 |
Jorge Almeida |
51 |
-- |
52 |
gentoo-user@g.o mailing list |