Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if X server is running?

Tags:

linux

x11

xserver

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

I've 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 programmatically 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.

like image 398
RomanM Avatar asked Mar 12 '09 01:03

RomanM


People also ask

How do you check the server is running or not?

Use the following steps to check server uptime by using the systeminfo command: Connect to your cloud server on the command line. Type systeminfo and press Enter. Look for the line that starts with Statistics since , which indicates the date and time when the uptime started.

How can I tell if X is running on Ubuntu?

If you want to check whether x11 is installed, run dpkg -l | grep xorg . If you want to check if x11 is currently running (if logged in) then run echo $XDG_SESSION_TYPE . Paste the output.

Where is X server in Linux?

The Xorg command is actually a symbolic link from the X command usually located in /usr/bin. You need to run the Xorg command as root and ensure that no X server is running.


2 Answers

I often need to run an X command on a server that is running many X servers, so the ps based answers do not work. Naturally, $DISPLAY has to be set appropriately. To check that that is valid, use xset q in some fragment like:

if ! xset q &>/dev/null; then     echo "No X server at \$DISPLAY [$DISPLAY]" >&2     exit 1 fi 

EDIT

Some people find that xset can pause for a annoying amount of time before deciding that $DISPLAY is not pointing at a valid X server (often when tcp/ip is the transport). The fix of course is to use timeout to keep the pause amenable, 1 second say.

if ! timeout 1s xset q &>/dev/null; then     ⋮ 
like image 129
bobbogo Avatar answered Oct 12 '22 23:10

bobbogo


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

like image 22
Ken Avatar answered Oct 12 '22 23:10

Ken