It does.  And substitute using process groups instead of process ids:

bg=$!
pgid=$(ps h -o "%r" -p ${bg})
...
kill -- -${pgid}

The ps command returns the process group of the background process.

But the reason this works is a bit of luck because the kill command is
the last command.  The kill command will kill all processes in the
same process group id.  The "sleep" child process will be in that
group as well as the sleep's parent shell.  But so will the script
itself.  So that kill is killing all the script's child processes and
itself.

For example, a script like this will kill off the child sleep process,
but will never echo "world" :

#!/bin/bash
( sleep 20 ) &
bg=$!
pgid=$(ps h -o "%r" -p ${bg})
echo "hello"
kill -- -${pgid}
echo "world"

The optimal solution would be to have some tool that does one of two things:
1) given a process id, kills all child processes (I would have thought
that this would be a option to kill)
... or
2) given a process id, returns a list of child pids that can be passed to kill.

For a reasonably good description of processes, process groups,
sessions, and threads:

http://www.win.tue.nl/~aeb/linux/lk/lk-10.html

And to view them all:

$ ps jx -L | sort -k4,4n -k3,3n -k1,1n -k2,2n -k7,7n -k5,5n | less -iX -S

Regards,
- Robert

On Tue, Mar 12, 2013 at 8:45 PM, David Dooling <[email protected]> wrote:
> Does it count as elegant if you use ps? Something like:
>
> sleep_pid=$(ps -ef | awk "\$3 == $bg && \$8 ~ /sleep/ { print $2 }")
> kill $bg $sleep_pid
>
> where the first awk conditional targets children of the background process.
> This may have a race condition if $sleep_pid responds to the kill before
> $bg.
>
>
>
> On Tue, Mar 12, 2013 at 7:03 AM, Robert Citek <[email protected]>
> wrote:
>>
>> Hello all,
>>
>> I have a script that runs out of crontab that occasionally runs longer
>> than some time limit.  I would like the script to log when and only
>> when it runs longer than some timelimit.
>>
>> Here was the initial prototype for the script:
>>
>> #!/bin/bash
>> runtime=10
>> sleeptime=30
>> (
>> sleep $sleeptime
>> date >> /tmp/log.txt
>> ) &
>> bg=$!
>> sleep $runtime
>> kill $bg
>>
>> This works for $runtime greater than $sleeptime.  However, when
>> $runtime is less than $sleeptime, killing the parent process orphans
>> the sleep command.  When I run this command several times, I end up
>> with lots of orphaned sleep processes.
>>
>> The quick-n-dirty solution was to substitute this for..loop for the
>> sleep command:
>>
>> for((i=0;i<$sleeptime;i++)) ; do sleep 1 ; done
>>
>> It works good-enough, but am looking for a more elegant solution.
>>
>> Any thoughts?
>>
>> Regards,
>> - Robert
>>
>> --
>> --
>> Central West End Linux Users Group (via Google Groups)
>> Main page: http://www.cwelug.org
>> To post: [email protected]
>> To subscribe: [email protected]
>> To unsubscribe: [email protected]
>> More options: http://groups.google.com/group/cwelug
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Central West End Linux Users Group" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
>
>
> --
> David Dooling
>
> --
> --
> Central West End Linux Users Group (via Google Groups)
> Main page: http://www.cwelug.org
> To post: [email protected]
> To subscribe: [email protected]
> To unsubscribe: [email protected]
> More options: http://groups.google.com/group/cwelug
> ---
> You received this message because you are subscribed to the Google Groups
> "Central West End Linux Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
-- 
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: [email protected]
To subscribe: [email protected]
To unsubscribe: [email protected]
More options: http://groups.google.com/group/cwelug
--- 
You received this message because you are subscribed to the Google Groups 
"Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to