[ale] How to tell if X is up?

Dan Newcombe Newcombe at mordor.clayton.edu
Fri Jul 23 11:16:15 EDT 1999


On Wed, 21 Jul 1999 linuxhat at mailandnews.com wrote:
> On 21 Jul 1999, Dave Brooks wrote:
> >Yup.  Do this:
> >elysium:[~]% ps aux | grep X
> >root     21976  1.0 16.3 24720 20900 ?       S    Jul20  13:48
> >/usr/bin/X11/X -a
> >dbrooks  24198  0.0  0.3  1152  440 pts/2    S    14:51   0:00 grep X

> Should have been more clear.  *from within a C program*, how can we tell if 
> X is up and accepting connections on the local machine?  (also, from within
> a script, the above would have to be just a little bit smarter....)

popen("ps aux |grep -v grep |grep X");
   :)
(actually, there is a second parm, and my syntax is probably wrong - but
like you said, it needs to be smarter.

Seriously,
#include <X11/Xlib.h>
#include <stdio.h>
int main(int argc,char *argv)
{
  Display *display;
  display=XOpenDisplay(NULL);
  if(display==(Display *) NULL) {
                printf("Server not accepting connections\n");
		return(1);
  }
  return(0);
}

This works for me.  If I set my DISPLAY variable to a valid X server
accepting connections, then it works.  If I set it to a machine without an
X server (ie. NT :) then it prints the string.

This should be more portable than checking for the existence of a file
someplace.

	-Dan






More information about the Ale mailing list