15

The Travis CI documentation says to run sleep 3 after starting xvfb to "give [it] some time to start". I couldn't find any reference to this delay in the man page. Is this cargo cult programming? If not, how do I poll rather than sleep to guarantee it's available?

l0b0
  • 50,672
  • 41
  • 197
  • 360

2 Answers2

8

By default Xvfb will create a Unix Domain socket for clients to connect. On my system this file socket file is created in /tmp/.X11-unix/. You could use inotifywait to listen for events in this directory. For example,

 $ inotifywait -e create /tmp/.X11-unix/

and then run Xvfb :9 (display 9, for example). When it is ready you should see

/tmp/.X11-unix/ CREATE X9

from the inotifywait which will terminate. You should now be able to connect to DISPLAY=:9.

meuh
  • 49,672
  • 2
  • 52
  • 114
  • This would do the job on a system where I can expect proper job control, but I'm trying to do this in a Travis CI configuration file. I don't think I can rely on being able to use background jobs and wait for them to finish. – l0b0 Sep 18 '16 at 18:32
  • 4
    A simple `while [ ! -e /tmp/.X11-unix/X99 ]; do sleep 0.1; done` actually [did the trick](https://travis-ci.org/l0b0/graphics/builds/160870536). Marking as solved because of the path tip. – l0b0 Sep 18 '16 at 18:45
4

For those who can run full-fledged scripts, another option would be to use xdpyinfo on desired display:

# Start Xvfb
sudo /usr/bin/Xvfb "${DISPLAY}" -screen 0 1280x800x24 &

# Wait for Xvfb
MAX_ATTEMPTS=120 # About 60 seconds
COUNT=0
echo -n "Waiting for Xvfb to be ready..."
while ! xdpyinfo -display "${DISPLAY}" >/dev/null 2>&1; do
  echo -n "."
  sleep 0.50s
  COUNT=$(( COUNT + 1 ))
  if [ "${COUNT}" -ge "${MAX_ATTEMPTS}" ]; then
    echo "  Gave up waiting for X server on ${DISPLAY}"
    exit 1
  fi
done
echo "  Done - Xvfb is ready!"

(Answer inspired by https://gist.github.com/tullmann/476cc71169295d5c3fe6).

Anton
  • 141
  • 5
  • Thanks! Just the single `xdpyinfo` line would be enough; the loop could be done in a million different ways. – l0b0 Jan 24 '18 at 02:57
  • Sure it can be done in many ways. Though, this snippet creates pretty-printed output that also gives you an idea of how much time it took for Xvfb to be ready. I wrote that for my own use, but realized others can benefit from that too. – Anton Jan 25 '18 at 02:39