How to check if Xvfb is (already) running on display :0?

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


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)"
else
    echo "Not running"
fi

Source: https://github.com/Bumblebee-Project/Bumblebee/blob/master/install-files/common-functions#L112