tags:

views:

800

answers:

4
+1  Q: 

X server running ?

Is there any way to find out if the current session user is running an Xserver (under Linux) ?

I'v started off with things like:

ps -e | grep X

but this doesn't work always

and one more thing I tried is checking the $DISPLAY variable

Are there any other ways to check this?

EDIT: Some people suggested using the $DISPLAY variables but what if the user fiddles with this variable ? what if he tries to do something and changes this variable and then when I check it, it no longer reflects an accurate state of the system. Is there no specific way to do this that will always return a correct answer ?

I found that it can be done programatically thus:

#include <X11/Xlib.h> 
int main()
    { exit(XOpenDisplay(NULL) ? 0 : 1);  } 

$ gcc -o xprobe xprobe.c -L/usr/X11R6/lib -lX11

But I am looking for a script way.

+2  A: 

I use

pidof X && echo "yup X server is running"

pgrep and $DISPLAY are other options.

Other considerations:

su then $DISPLAY will not be set. Things that change the environment of the program running can make this not work.

I don't recommand ps -e | grep X as this will find procX, which is not the X server.

Ian Kelling
Isn't this the same as ps -e | grep X ?AFAIK pidof just an alias for killall5 and is not always defined on all systems
Roman M
See my updated answer.
Ian Kelling
From man pidof:"pidof is actually the same program as killall5; the program behaves according to the name under which it is called."pidof behaves differently than killall5. If your using an old old unix system it may not be there.
Ian Kelling
But the same thing can be said about old unix systems regarding about a million things.
Ian Kelling
+5  A: 

$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.

Ken
but what if the user fiddles with this variable ? what if he tries to do something and changes this variable and then when I check it, it no longer reflects an accurate state of the system. Is there no concrete way to do this?
Roman M
If I set $DISPLAY, it's because I want your program to go to a different display. What exactly is your use case where you want to second-guess the user's explicit configuration? Do you avoid using $HOME in case the user "fiddles" with that, too? Where does it end?
Ken
i don't want to second guess, just bullet-proof. The use case is for a driver installer, one of the prerequisites is having x running.And I wouldn't avoid using $HOME but ill check it points to an exsisting directory first just in case ^^
Roman M
Which X server, exactly, do you want to access? I've many times had to install drivers on remote systems. Also, in POSIX XOpenDisplay(NULL) is defined to simply read $DISPLAY, so at best it sounds like you're counting on nonstandard behavior.
Ken
There is not particular X I want to detect, my installer just needs to know that the user is running it with one present.
Roman M
I mean, if there's one running on localhost but they set $DISPLAY to another server because they're logged in remotely, do you still want the "local" one? What if there's more than one local one? I don't know how you'd detect that, or why you'd want to. $DISPLAY exists for a reason.
Ken
A: 

One trick I use to tell if X is running is:

telnet 127.0.0.1 6000

If it connects, you have an X server running and its accepting inbound TCP connections (not usually the default these days)....

dicroce
A: 

1)

# netstat -lp|grep -i x
tcp        0      0 *:x11                   *:*                     LISTEN      2937/X          
tcp6       0      0 [::]:x11                [::]:*                  LISTEN      2937/X          
Active UNIX domain sockets (only servers)
unix  2      [ ACC ]     STREAM     LISTENING     8940     2937/X              @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     8941     2937/X              /tmp/.X11-unix/X0
#

2) nmap

# nmap localhost|grep -i x
6000/tcp open  X11
#
vitaly.v.ch