Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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:

#/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"
share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.