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

发布于 2024-07-15 02:35:09 字数 534 浏览 8 评论 0原文

有什么方法可以查明当前会话用户是否正在运行 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 

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

九歌凝 2024-07-22 02:35:09

我经常需要在运行许多 X 服务器的服务器上运行 X 命令,因此基于 ps 的答案不起作用。 当然,$DISPLAY 必须进行适当的设置。 要检查其是否有效,请在某些片段中使用xset q,例如:

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

编辑

有些人发现xset可以在决定$之前暂停一段烦人的时间DISPLAY 未指向有效的 X 服务器(通常当 tcp/ip 是传输时)。 当然,解决方法是使用超时来保持暂停的顺从性,比如1秒。

if ! timeout 1s xset q &>/dev/null; then
    ⋮

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
    ⋮
妥活 2024-07-22 02:35:09

$DISPLAY 是标准方式。 这就是用户与程序沟通要使用哪个 X 服务器(如果有)的方式。

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

宛菡 2024-07-22 02:35:09

您可以使用xdpyinfo(可以通过apt-get install x11-utils安装)。

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

三生一梦 2024-07-22 02:35:09

我用来判断 X 是否正在运行的一个技巧是:

telnet 127.0.0.1 6000

如果它连接,则说明您有一个 X 服务器正在运行并且它接受入站 TCP 连接(现在通常不是默认设置)......

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

甜尕妞 2024-07-22 02:35:09

我使用

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

pgrep 和 $DISPLAY 是其他选项。

其他注意事项:

su 那么 $DISPLAY 将不会被设置。 改变程序运行环境的事情可能会使其不起作用。

我不推荐 ps -e | grep X,因为这会找到 procX,它不是 X 服务器。

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.

霓裳挽歌倾城醉 2024-07-22 02:35:09
xprop -root &> /dev/null 

...是我尝试过的& true 命令用于测试“X-able”情况。 而且,它几乎可以保证在任何运行 X 的系统上,当然,如果无论如何都找不到该命令,该命令就会失败,所以即使它不存在,您也几乎可以假设也不存在 X。 (这就是为什么我使用 &> 而不是 >)

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

栖竹 2024-07-22 02:35:09

我编写了 xdpyprobe 程序就是为了这个目的。 与 xset、xdpyinfo 和其他通用工具不同,它不执行任何额外操作(仅检查 X 服务器并退出),并且可能不会产生任何输出(如果指定了“-q”选项)。

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

浅暮の光 2024-07-22 02:35:09

bash 脚本解决方案:

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

如果从另一个控制台(Ctrl+Alt+F?)或 ssh 登录,则不起作用。 对我来说,这个解决方案适用于我的 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

您可以仅将 /usr/lib/Xorg 更改为 Xorg 或系统上的正确命令。

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.

那一片橙海, 2024-07-22 02:35:09

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
#

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
#
慕巷 2024-07-22 02:35:09

首先,您需要确保基础 X11 软件包已正确安装在您的服务器上:

rpm -qa | grep xorg-x11-xauth

如果没有,请安装所有软件包:

sudo yum install xorg-x11-xauth xterm

确保 openssh 服务器配置为转发 x11 连接:

edit file : vim /etc/ssh/sshd_config

X11Forwarding yes

注意:如果该行前面有注释 (#) 或设置为 no,更新文件以匹配上述内容,然后重新启动 ssh 服务器守护程序(此处要小心 - 如果您犯了错误,您可能会将自己锁定在服务器之外)

sudo /etc/init.d/sshd restart

现在,配置 SSH 应用程序以转发 X11 请求:

ssh -Y your_username@your_server.your_domain.com

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
迷鸟归林 2024-07-22 02:35:09
if [[ $DISPLAY ]]; then 
  …
fi
if [[ $DISPLAY ]]; then 
  …
fi
耳根太软 2024-07-22 02:35:09

这是用于检查的 PHP 脚本。

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

类似的命令也可以在 shell 脚本中使用。 就像 pidof 命令一样。

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.

江南烟雨〆相思醉 2024-07-22 02:35:09

其他答案都不是在这十年(2020 年代)写的,其中大多数都过于复杂或只是简单奇怪且不起作用,并且没有一个针对 Wayland 进行测试。

这是一个更好的解决方案:

ps aux | grep -e 'wayland\|Xorg' | grep -e 'wayland\|Xorg' | grep -v grep

这将搜索 Xwayland 或 Xorg 并删除误报的 grep 搜索。 它还会找到 wayland 独立版和 Xwayland

None of the other answers were written in this decade (the 2020s), most of them are overly-complex or just plain weird and nonfunctional, and none of them test for Wayland.

Here's a much better solution:

ps aux | grep -e 'wayland\|Xorg' | grep -v grep

This searches for either Xwayland or Xorg and removes the false-positive grep searches. It also will find both wayland standalone and Xwayland.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文