Linux 如何检查 X 服务器是否正在运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/637005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if X server is running?
提问by RomanM
Is there any way to find out if the current session user is running an Xserver (under Linux) ?
有没有办法查明当前会话用户是否正在运行 Xserver(在 Linux 下)?
I'v 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
我尝试的另一件事是检查$DISPLAY
变量
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 ?
编辑:有些人建议使用 $DISPLAY 变量,但如果用户摆弄这个变量怎么办?如果他尝试做某事并更改此变量,然后当我检查它时,它不再反映系统的准确状态,该怎么办。是否没有特定的方法可以始终返回正确的答案?
I found that it can be done programatically 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.
但我正在寻找一种脚本方式。
回答by Ian Kelling
I use
我用
pidof X && echo "yup X server is running"
pgrep and $DISPLAY are other options.
pgrep 和 $DISPLAY 是其他选项。
Other considerations:
其他注意事项:
su then $DISPLAY will not be set. Things that change the environment of the program running can make this not work.
su 那么 $DISPLAY 将不会被设置。改变程序运行环境的事情可能会使这不起作用。
I don't recommand ps -e | grep X as this will find procX, which is not the X server.
我不推荐 ps -e | grep X 因为这会找到 procX,它不是 X 服务器。
回答by Ken
$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.
$DISPLAY 是标准方式。这就是用户与程序通信关于使用哪个 X 服务器(如果有)的方式。
回答by dicroce
One trick I use to tell if X is running is:
我用来判断 X 是否正在运行的一个技巧是:
telnet 127.0.0.1 6000
远程登录 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)....
如果它连接,则您有一个 X 服务器正在运行并且它接受入站 TCP 连接(这些天通常不是默认设置)...
回答by vitaly.v.ch
1)
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
2) 映射
# nmap localhost|grep -i x 6000/tcp open X11 #
回答by bobbogo
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:
我经常需要在运行许多 X 服务器的服务器上运行 X 命令,因此ps
基于的答案不起作用。当然,$DISPLAY
必须适当设置。要检查它是否有效,请xset q
在某些片段中使用,例如:
if ! xset q &>/dev/null; then
echo "No X server at $DISPLAY [$DISPLAY]" >&2
exit 1
fi
回答by shuckc
You can use xdpyinfo
(can be installed via apt-get install x11-utils
).
您可以使用xdpyinfo
(可以通过 安装apt-get install x11-utils
)。
回答by osirisgothra
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 >)
...是我尝试过的测试“X-able”情况的真实命令。而且,它几乎可以保证在任何运行 X 的系统上,当然,如果找不到该命令就会失败,因此即使它不存在,您也几乎可以假设没有 X。(这就是我使用 &> 而不是 > 的原因)
回答by Alex Kost
I wrote xdpyprobeprogram 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).
我编写了用于此目的的xdpyprobe程序。与 xset、xdpyinfo 和其他通用工具不同,它不执行任何额外操作(仅检查 X 服务器并退出)并且可能不会产生任何输出(如果指定了“-q”选项)。
回答by user3607430
This is PHP script for checking.
这是用于检查的 PHP 脚本。
$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.
类似的命令也可以在 shell 脚本中使用。就像 pidof 命令一样。
回答by Srijan Chaudhary
First you need to ensure foundational X11 packages are correctly installed on your server:
首先,您需要确保在您的服务器上正确安装了基础 X11 软件包:
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 :
确保 openssh 服务器配置为转发 x11 连接:
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)
注意:如果该行前面有注释 (#) 或设置为 no,请更新文件以匹配上述内容,然后重新启动 ssh 服务器守护程序(此处要小心 — 如果您犯了错误,您可能会将自己锁定在服务器)
sudo /etc/init.d/sshd restart
Now, configure SSH application to forward X11 requests :
现在,配置 SSH 应用程序以转发 X11 请求:
ssh -Y your_username@your_server.your_domain.com