如何检查 X 服务器是否正在运行?

有没有办法查明当前会话用户是否正在运行 Xserver (在 Linux 下) ?

我开始的时候是这样的:

ps -e | grep X

但这并不总是奏效

我还试过检查 $DISPLAY变量

还有别的办法吗?

编辑:

有些人建议使用 $DISPLAY变量,但是如果用户改动这个变量会怎么样呢?如果他尝试做一些事情,并改变这个变量,然后当我检查它时,它不再反映系统的精确状态。 有没有具体的方法可以总是返回一个正确的答案?

我发现它可以通过编程方式实现:

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


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

但我正在寻找一个脚本的方式。

237698 次浏览

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.

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

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

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
#

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
⋮

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

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

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

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.

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

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.