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.


Reply via email to