问题描述
有没有办法找出当前会话用户是否正在运行Xserver(在Linux下)?
Is there any way to find out if the current session user is running an Xserver (under Linux) ?
我从以下事情开始:
ps -e | grep X
但这并不总是有效
我尝试的另一件事是检查$DISPLAY
变量
and one more thing I tried is checking the $DISPLAY
variable
还有其他方法可以检查吗?
Are there any other ways to check this?
有人建议使用$ DISPLAY变量,但是如果用户摆弄这个变量怎么办?如果他尝试执行某项操作并更改此变量,然后在我进行检查时,该变量不再反映系统的准确状态,该怎么办?没有特定的方法可以始终返回正确答案吗?
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 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.
推荐答案
我经常需要在运行许多X服务器的服务器上运行X命令,因此基于ps
的答案不起作用.自然,必须适当设置$DISPLAY
.要检查其是否有效,请在某些片段中使用xset q
,例如:
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
这篇关于如何检查X服务器是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!