90

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.

8
  • 2
    why doesn't "ps -e | grep X" always work?
    – stephendl
    Mar 12, 2009 at 1:18
  • 3
    And why doesn't $DISPLAY work? Mar 12, 2009 at 1:22
  • @stephendl:I found that in some situations X is running but the user is connected through a terminal with no X @matthew: What if the user fiddles with this variable and it no longer reflects the actual state of the system ?
    – RomanM
    Mar 12, 2009 at 1:39
  • Just a nit pick: there's no point in calling exit() in your C program to test for X, just return the status and you're done.
    – unwind
    Mar 12, 2009 at 9:04
  • 6
    And to nitpick a little more... the XOpenDisplay makes use of the DISPLAY environment variable internally. Mar 12, 2009 at 9:07

12 Answers 12

84

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
    ⋮
11
  • I think this is "better" than using xprop and xdpyinfo since xset produces less output for /dev/null to eat. The OP should accept this answer.
    – jamadagni
    Nov 20, 2015 at 3:44
  • 2
    @will This hang won't be specific to xset. Any X program will suffer it. The hang depends on the X transport. If you have DISPLAY=:1 say, this implies tcp port 6001. You may have some rouge process listening. My xset returns immediately when nothing is listening.
    – bobbogo
    Aug 30, 2018 at 15:07
  • 1
    @dstromberg This is a pretty inappropriate place for this question! Nevertheless: it looks to me like you are ssh-ing from a local machine local into a remote machine remote, with X-forwarding turned on in your ssh. The problem is that the original ssh command needs access to a working X server, and it looks to me that you have a badly set $DISPLAY on local. What then happens is you run an X command on remote. Ssh tunnels the X protocol back to local which fails (due to the dodgy $DISPLAY) and ssh prints the message.
    – bobbogo
    Jul 1, 2019 at 12:31
  • 1
    This takes 35 seconds in my WSL2 (Windows Subsystem for Linux) to finish when the X server is stopped. Is it any faster (instantaneously) way just to check if the X server is available? Thx. Jul 30, 2020 at 7:42
  • 1
    Hi @bobbogo I'm trying with MobaXterm and I set DISPLAY to this (which give me access from Windows): export DISPLAY="$(/sbin/ip route | awk '/default/ { print $3 }'):0". I need to run xmodmap several times on shell startup (.profile) which takes a lot to answer if X server is down. I just want to add a guard in the script to prevent it. The guard is about test if X-server is running. Jul 30, 2020 at 10:55
21

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

9
  • 6
    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
    Mar 12, 2009 at 1:53
  • 2
    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
    Mar 12, 2009 at 2:20
  • 6
    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
    Mar 12, 2009 at 4:14
  • 1
    Could you elaborate your answer for users not from the Linux world? What is $DISPLAY? How to use it?
    – Paul
    May 21, 2019 at 11:39
  • 2
    Under Windows (WSL), I want to test whether X is already running or not. In other words, I want to test BECAUSE I want to set DISPLAY. I may rephrase DISPLAY is set by something that assumes it knows X is running, it has nothing to do with the fact it is actually running.
    – Champignac
    Dec 10, 2020 at 12:08
9

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

2
  • 1
    I guess at present, security-conscious systems would not have the telnet command available.
    – boardrider
    Jun 11, 2017 at 11:47
  • 2
    Security-conscious systems won't be listening on a TCP port either, but would be restricted to Unix domain sockets, further invalidating this method.
    – alanc
    Jan 5, 2019 at 18:28
9

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.

3
  • 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
    – RomanM
    Mar 12, 2009 at 1:42
  • 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. Mar 12, 2009 at 2:51
  • But the same thing can be said about old unix systems regarding about a million things. Mar 12, 2009 at 2:55
9

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

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

3
  • 3
    Re &> for use in shell scripts running under dash which does not support &>, one can use >/dev/null 2>&1.
    – jamadagni
    Nov 20, 2015 at 3:47
  • 2
    On cygwin a typical way to install X is install the package xinit. So, xprop is not installed automatically as it is not a dependency. Apr 26, 2016 at 7:34
  • 1
    Take ~500ms to run, while xset q only takes 300
    – Andy
    Feb 12, 2021 at 21:16
4

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

4

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.

1
  • 1
    in my case the process name was /usr/lib/xorg/Xorg instead of /usr/lib/Xorg
    – ignacio
    Oct 27, 2020 at 13:52
2

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
#
2

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
1
if [[ $DISPLAY ]]; then 
  …
fi
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.