Super User is a question and answer site for computer enthusiasts and power users. 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

I need to give a command like

Xvfb :6 -screen 0 1024x768x24

Where 6 can be a display number. Now Xvfb can already be using that Display, so how do I get the first display number not in use?

share|improve this question
up vote 0 down vote accepted

There may be an easier way, but I'd write a script that queries the displays with xdpyinfo.

There are pretty much 3 cases:

  1. The display exists, and your shell has access to the display. xdpyinfo will spit out lots of info.

  2. The display exists, and your shell doesn't have access to the display. xdpyinfo will spit out an access error and an unable-to-open error, like this:
    Invalid MIT-MAGIC-COOKIE-1 keyxdpyinfo: unable to open display ":0".

  3. The display doesn't exist. xdpyinfo will spit out an unable-to-open error:
    xdpyinfo: unable to open display ":1".

A script would look a bit like this:


while [ "$DONE" == "no" ]
   out=$(xdpyinfo -display :$CHECKDISPLAY 2>&1)
   if [[ "$out" == name* ]] || [[ "$out" == Invalid* ]]
      # command succeeded; or failed with access error;  display exists
      (( CHECKDISPLAY+=1 ))
      # display doesn't exist

echo "first available display is :$CHECKDISPLAY"
share|improve this answer

xvfb-run has a flag --auto-servernum which works by looking for the X lock files in /tmp.

Squished into a single line that you could alias in your .bashrc, it looks like this:

i=0; while [ -f /tmp/.X$i-lock ]; do i=$(($i + 1)); done; echo $i

If you are simply trying to run a program with the display set to Xvfb, I would just use xvfb-run.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .