Checking if the X Server is running

Phil Betts Phil.Betts@ascribe.com
Mon Oct 1 16:38:00 GMT 2007


O. Olson wrote on Monday, October 01, 2007 1:49 AM::

> 	I still don't think I can get this to work. I tried
> this from the command line (and my file is in
> /usr/local/bin/sd.sh)
> 
> C:\cygwin\bin\bash --login /usr/local/bin/sd.sh
> 
> This still brings up the Fatal Error Window.
> 

It's probably not the cause of your problem, but you should
never use "ps | grep xxx" to detect if a process is running.
This is because the grep process will (sometimes) detect 
itself and give you a false positive, and your xterm will 
try to start when there is no server running.

Since you already installed checkx, that's what you should 
be using, because (I believe) it actually connects to the X
server.

What you may be experiencing, is a race condition between
whatever process actually started X and the sd.sh.

I.e. if you initially start X using startxwin.bat (or similar),
and it hasn't got as far as starting the server when sd.sh 
checks if it's running, sd.sh will try to start the server.  
However, by the time sd.sh gets round to starting X, the 
first server has started, and therefore the second gives you 
the error.

Try something like this:

----------------------------------------------------
#!/bin/bash -l

# wait up to 5 seconds before deciding if X needs starting. You may
# need to up this to 10 seconds or more depending on your system.
wait_for_x ()
{
    x_down=1
    for (( i=0 ; i < 5 ; i++ ));do
        echo "waiting for X"
        if checkx;then
            x_down=0
            break
        fi
        sleep 1
    done
    return $x_down
}

if ! wait_for_x;then
  # X not yet up
  run XWin ....         # see [1]
  wait_for_x            # see [2]
fi

exec xterm
----------------------------------------------------

[1] I don't advise running XWin directly.  It is better to use 
startxwin.sh.  This ensures that the required environment variables 
are set up, and that any stray socket left from an earlier unclean 
exit is cleaned up.  The script does what it does for good reasons.
Of course the default startxwin.sh starts xterm anyway, so you may
want to edit it.  (IMHO it was a big mistake to start xterm in the 
server start scripts.  It confuses way too many newbies, who think 
the scripts are the proper way to start xterm, then complain when 
they get errors trying to open a second xterm.)

[2] The run command returns control to bash before XWin has finished 
initialising, so the wait is necessary to ensure that xterm isn't
started before the server is usable.


Phil

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://x.cygwin.com/docs/
FAQ:                   http://x.cygwin.com/docs/faq/



More information about the Cygwin-xfree mailing list