How to check if X server is running?

LinuxX11Xserver

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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
    ⋮

Solution 2 - Linux

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

Solution 3 - Linux

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.

Solution 4 - Linux

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)....

Solution 5 - Linux

You can use xdpyinfo (can be installed via apt-get install x11-utils).

Solution 6 - Linux

xprop -root &> /dev/null 

...is my tried & true command to test for an "X-able" situation. And, it's pretty much guaranteed to be on any system running X, of course, the command fails if not found anyways, so even if it doesnt exist, you can pretty much assume there is no X either. (thats why I use &> instead of >)

Solution 7 - Linux

I wrote xdpyprobe program which is intended for this purpose. Unlike xset, xdpyinfo and other general tools, it does not do any extra actions (just checks X server and exits) and it may not produce any output (if "-q" option is specified).

Solution 8 - Linux

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

  1. nmap

nmap localhost|grep -i x

6000/tcp open X11

Solution 9 - Linux

The bash script solution:

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

Doesn't work if you login from another console (Ctrl+Alt+F?) or ssh. For me this solution works in my Archlinux:

#!/bin/sh
ps aux|grep -v grep|grep "/usr/lib/Xorg"
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
  echo "X server running"
  exit 1
fi

You can change /usr/lib/Xorg for only Xorg or the proper command on your system.

Solution 10 - Linux

First you need to ensure foundational X11 packages are correctly installed on your server:

rpm -qa | grep xorg-x11-xauth

If not then, kindly install all packages :

sudo yum install xorg-x11-xauth xterm

Ensure that openssh server is configured to forward x11 connections :

edit file : vim /etc/ssh/sshd_config

X11Forwarding yes

NOTE : If that line is preceded by a comment (#) or is set to no, update the file to match the above, and restart your ssh server daemon (be careful here — if you made an error you may lock yourself out of the server)

sudo /etc/init.d/sshd restart

Now, configure SSH application to forward X11 requests :

ssh -Y your_username@your_server.your_domain.com

Solution 11 - Linux

if [[ $DISPLAY ]]; then 
  …
fi

Solution 12 - Linux

This is PHP script for checking.

$xsession = `pidof X`;
if (!$xsession) {
	echo "There is no active X session, aborting..\n";
	exit;
}

Similar command can be used in shell script too. like the pidof command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomanMView Question on Stackoverflow
Solution 1 - LinuxbobbogoView Answer on Stackoverflow
Solution 2 - LinuxKenView Answer on Stackoverflow
Solution 3 - LinuxIan KellingView Answer on Stackoverflow
Solution 4 - LinuxdicroceView Answer on Stackoverflow
Solution 5 - LinuxshuckcView Answer on Stackoverflow
Solution 6 - LinuxosirisgothraView Answer on Stackoverflow
Solution 7 - LinuxAlex KostView Answer on Stackoverflow
Solution 8 - Linuxvitaly.v.chView Answer on Stackoverflow
Solution 9 - LinuxomlView Answer on Stackoverflow
Solution 10 - LinuxSrijan ChaudharyView Answer on Stackoverflow
Solution 11 - LinuxSerge StroobandtView Answer on Stackoverflow
Solution 12 - Linuxuser3607430View Answer on Stackoverflow