5

I've read man-page X(7) and searched for this but I can't find a solution.

How can I check in a long running process if the environment DISPLAY=:0 is still a valid display? (User could have logged out in the meantime but process is still alive)

The icing on the cake would be to check if the DISPLAY is still owned by <USER>

Germar
  • 377

2 Answers2

6

Maybe something like:

if sudo -Hu "$user" xdpyinfo -display "$DISPLAY" > /dev/null 2>&1; then
  echo "user $user can connect to display $DISPLAY"
else
  echo "user $user cannot connect to display $DISPLAY"
fi

If you don't have xdpyinfo, you can try any of those standard simple X11 utilities that don't display anything on the X server (with their stdout and stderr redirected to /dev/null like above):

xlsatoms -name PRIMARY
xprop -notype -root CUT_BUFFER0
xwininfo -root
4

If :0 ever was a valid display, the process, i.e. xserver providing it, would be owned by root. Whatever you mean by valid.

If you want to know if you can connect to an xserver, I'd suggest to use xlsclients to connect to that display. If the command can connect it will return 0, if it can't it will return 1.

X does not work in a manner that one user connects once to one xserver. To tie users to xsessions you must look at the programs connected to the xserver. Windowmanagers are good candidates for that, b/c there can be only one (active) windowmanager at any time. You can also track process relationships (parent,child) starting from your display manager. However, it's never trivial. Maybe display managers store this information somewhere, but that I wouldn't know.

Bananguin
  • 7,984