问题
Is there any way to find out if the current session user is running an Xserver (under 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
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 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.
回答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, use xset q
in some fragment like:
if ! xset q &>/dev/null; then
echo "No X server at \$DISPLAY [$DISPLAY]" >&2
exit 1
fi
回答2:
$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.
回答3:
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.
回答4:
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)....
回答5:
You can use xdpyinfo
(can be installed via apt-get install x11-utils
).
回答6:
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 #
回答7:
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 >)
回答8:
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).
回答9:
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
回答10:
if [[ $DISPLAY ]]; then
…
fi
回答11:
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.
回答12:
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.
来源:https://stackoverflow.com/questions/637005/how-to-check-if-x-server-is-running