如何检查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
但我正在寻找一种脚本方式。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我经常需要在运行许多 X 服务器的服务器上运行 X 命令,因此基于
ps
的答案不起作用。 当然,$DISPLAY
必须进行适当的设置。 要检查其是否有效,请在某些片段中使用xset q
,例如:编辑
有些人发现xset可以在决定
$之前暂停一段烦人的时间DISPLAY
未指向有效的 X 服务器(通常当 tcp/ip 是传输时)。 当然,解决方法是使用超时来保持暂停的顺从性,比如1秒。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, usexset q
in some fragment like: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.$DISPLAY 是标准方式。 这就是用户与程序沟通要使用哪个 X 服务器(如果有)的方式。
$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.
您可以使用
xdpyinfo
(可以通过apt-get install x11-utils
安装)。You can use
xdpyinfo
(can be installed viaapt-get install x11-utils
).我用来判断 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)....
我使用
pgrep 和 $DISPLAY 是其他选项。
其他注意事项:
su 那么 $DISPLAY 将不会被设置。 改变程序运行环境的事情可能会使其不起作用。
我不推荐 ps -e | grep X,因为这会找到 procX,它不是 X 服务器。
I use
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.
...是我尝试过的& true 命令用于测试“X-able”情况。 而且,它几乎可以保证在任何运行 X 的系统上,当然,如果无论如何都找不到该命令,该命令就会失败,所以即使它不存在,您也几乎可以假设也不存在 X。 (这就是为什么我使用 &> 而不是 >)
...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 >)
我编写了 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).
bash 脚本解决方案:
如果从另一个控制台(Ctrl+Alt+F?)或 ssh 登录,则不起作用。 对我来说,这个解决方案适用于我的 Archlinux:
您可以仅将 /usr/lib/Xorg 更改为 Xorg 或系统上的正确命令。
The bash script solution:
Doesn't work if you login from another console (Ctrl+Alt+F?) or ssh. For me this solution works in my Archlinux:
You can change /usr/lib/Xorg for only Xorg or the proper command on your system.
1)
2) nmap
1)
2) nmap
首先,您需要确保基础 X11 软件包已正确安装在您的服务器上:
如果没有,请安装所有软件包:
确保 openssh 服务器配置为转发 x11 连接:
注意:如果该行前面有注释 (#) 或设置为 no,更新文件以匹配上述内容,然后重新启动 ssh 服务器守护程序(此处要小心 - 如果您犯了错误,您可能会将自己锁定在服务器之外)
现在,配置 SSH 应用程序以转发 X11 请求:
First you need to ensure foundational X11 packages are correctly installed on your server:
If not then, kindly install all packages :
Ensure that openssh server is configured to forward x11 connections :
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)
Now, configure SSH application to forward X11 requests :
这是用于检查的 PHP 脚本。
类似的命令也可以在 shell 脚本中使用。 就像 pidof 命令一样。
This is PHP script for checking.
Similar command can be used in shell script too. like the pidof command.
其他答案都不是在这十年(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 bothwayland
standalone andXwayland
.