1

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?

2 Answers 2

0

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:

#/bin/bash
CHECKDISPLAY=0
DONE="no"

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

echo "first available display is :$CHECKDISPLAY"
2

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.

You must log in to answer this question.

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