Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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>

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Maybe something like:

if sudo -Hu "$user" xdpyinfo -display "$display" > /dev/null 2>&1; then
  echo "user $user can connect to display $display"
  echo "user $user cannot connect to display $display"
share|improve this answer
when xdpyinfo isn't installed, you can use xterm -iconic -e exit instead.... –  Erik Aronesty Jun 26 at 18:09

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.