Patrick Wade wrote,
>A shell script runs a for loop iterating a command:
>
>#!/bin/sh
># foo.sh ; an example script
>
>for i in foo bar baz
>       do find /home/$i
>       done
>
>exit 0
>
>If we run this and observe top, we will see three processes; one for
>foo.sh, another for a child foo.sh (the for loop) and one for find.
>
>If i send a kill to the parent foo.sh process, the child foo.sh and find
>jobs will continue running.  This, from what i gather, is normal *NIX
>behavior.  My objective, however, is to ensure that foo.sh exits by a
>certain time, so i have an at job waiting to send it a kill.  Is there
>an idiom when writing the script to ensure that the kill it receives
>will kill the child processes as well?  Is there a completely different
>approach i need to take when the design requirement of termination by a
>time certain is added?  Exhortations to learn Python are perhaps apropos
>but useless in the short term.

How about something like this:

     #!/bin/sh
     # foo.sh: an example script
     trap 'kill -HUP $!;exit 1' 1 2 3 15
     for i in foo bar baz
     do find /home/$i &
        wait
     done
     exit 0

This makes sure the signal (if it's HUP, INT, QUIT, or TERM) propogates
to the find command.  The funny business with the "&" and the "wait" is
so we can get at find's pid using "$!".

Be careful of the trap command...in order to work, the first argument must
be in SINGLE quotes.

              - Neil Parker
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug

Reply via email to