bash script help

David Thornton david-FkEgs2FKm2NvBvnq28/GKQ at public.gmane.org
Mon Jun 21 11:55:55 UTC 2004


Noah John Gellner wrote:

>I am using a script to preload openoffice so that it starts more
>quickly. The script works very well except that when I exit X to get to
>the console the script keeps running and outputting messages to the
>display reporting that no display is found. 
>
>I am hoping that someone can tell me how to modify the script to test if
>X is running, and if not to terminate the loop. 
>
>The script comes from the Gentoo forums and has two parts
>
>Part one is called by Gnome Sessions:
>
>## killing all traces of openoffice
>killall -9 ooresident
>killall -9 soffice.bin
>
>## waiting for them to really dissapear (can even take a sec or 2
>depending
>## on the busy-ness and speed of your system)
>sleep 1
>
>## the actual keepalive is called in the bg
>ooresident &
>
>Part two, called oowresident, is obviously called by part one:
>#!/bin/bash
>killall -9 soffice.bin ## just in case....
>i=0
>while [ $i -eq 0 ]
>echo $i
>do
>{
>      dummy=`ooffice -plugin -quickstart ;`
>      i=$?
>      echo $i
>      echo "OpenOffice was unloaded. Loading it again..."
>}
>done
>			
>Beside the problem with the script not terminating with X, it works
>really well, shorting OO startup times to a few seconds.
>
>Help is greatly appreciated.
>
>Cheers,
>Noah
>  
>
I've read all the notes on ooqs and such , but I thought I would chime 
in with my two cents.

I have often written scripts that need to wait for a processes to leave 
the process tree. Earlier versiions of Tomcat is notorious :)

function waitfor {

        # $1 is the process id to wait for
        # $2 is the timeout

        RETURN=

        if [ $# -lt 2 ] ; then
                echo "nothing to wait for"
                echo "you passed \"waitfor\" the wrong args"
                RETURN=1
        else
                echo "ok I'll wait for $1 for $2 seconds"
                echo Still running = 1
                STILL_RUNNING="1"
                COUNT="1"
                while [ $STILL_RUNNING -gt 0 ] && [ $COUNT -lt $2 ]
                        do
                                STILL_RUNNING=`ps --pid $1 | wc -l`
                                STILL_RUNNING=$[$STILL_RUNNING-1];
                                echo "STILL_RUNNING = $STILL_RUNNING"
                                COUNT=$[$COUNT+1];
                                echo "COUNT = $COUNT";
                                echo "Timeout is $2 seconds";
                                echo "waiting..."
                                sleep 1
                                echo "done sleep"
                        done

                if [ $STILL_RUNNING -gt 0 ] ; then
                        RETURN=1
                        echo "the function "waitfor" timed out waiting 
for $1,";
                        echo "it waited for $2"
                else
                        RETURN=0
                fi
        fi
        return $RETURN
}


david
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list