Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Title says it all. pgrep Xvfb only does half of the trick. It will not tell me the display number.

share|improve this question
up vote 14 down vote accepted

I'm not sure what you're trying to accomplish, but you can check whether a X Display is available by running "x display info":

xdpyinfo -display :0 >/dev/null 2>&1 && echo "In use" || echo "Free"

Note: if you cannot access the display, it'll be reported as "Free" too. Another way is checking for the existence of /tmp/.X0-lock which contains the PID file of a X server.

Instead of displaying the command, a more reliable way would be checking for processes matching the binary:

pids=$(pidof /usr/bin/Xvfb)
if [ -n "$pids" ]; then
    processes="$(ps --format command --no-headers -ww --pid $pids)"
    echo "Not running"


share|improve this answer
xdpyinfo does the trick for me, thanks! Btw. I want to set the DISPLAY env variable based on Xvfb running on :0. – Paweł Gościcki Sep 9 '11 at 8:29
So you're trying to get the display number used by Xvfb? Try using the ps command in my answer and use something like display=":${processes##*:}";display="${%% *}" to get the display number (assuming it's given with the command which is done for Xorg at least) – Lekensteyn Sep 9 '11 at 11:51
I will have it running only on :0, so simple check for :0 is sufficient for my needs. Thanks again! – Paweł Gościcki Sep 9 '11 at 17:10

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.