I have some unit tests that need an X11 display so I plan to start Xvfb before running them, but to start Xvfb I will need a free display number to connect it to. My best guess
Based in the answer of @karunski.
Using Xvfb to probe the displays, and lsof to check if are unix sockets in the Xvfb process, is more effective, notice the sleep 0.5
, can be variable depends on the machine.
#!/bin/bash
DISPLAY=0
until [ $DISPLAY_NUM > 10 ]; do
echo -n "Looking for display on $DISPLAY..."
Xvfb :$DISPLAY > /dev/null 2>&1 &
pid=$!
sleep 0.5
lsof -a -U -p $pid > /dev/null 2>&1
notfound="$?"
kill $pid > /dev/null 2>&1
wait $pid
[ "$notfound" == "0" ] && echo "found" && break
echo "fail"
let DISPLAY=DISPLAY+1
done