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.
Use the following steps to check server uptime by using the systeminfo command: Connect to your cloud server on the command line. Type systeminfo and press Enter. Look for the line that starts with Statistics since , which indicates the date and time when the uptime started.
If you want to check whether x11 is installed, run dpkg -l | grep xorg . If you want to check if x11 is currently running (if logged in) then run echo $XDG_SESSION_TYPE . Paste the output.
The Xorg command is actually a symbolic link from the X command usually located in /usr/bin. You need to run the Xorg command as root and ensure that no X server is running.
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
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 ⋮
$DISPLAY is the standard way. That's how users communicate with programs about which X server to use, if any.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With