Find a free X11 display number

前端 未结 5 1815
旧巷少年郎
旧巷少年郎 2021-02-19 23:51

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

5条回答
  •  伪装坚强ぢ
    2021-02-20 00:31

    There's no point in trying to find a free display number. As you have guessed, between the time you find a free one and the time Xvfb starts, another X server might have taken the port you thought was free. So, better to just try to launch Xvfb, handle the failure if the port is taken, and then retry at the next port until you succeed or run out of ports to try.

    #!/bin/bash
    DISPLAY_NUM=0
    unset TEST_HAS_RUN
    until [ $TEST_HAS_RUN ] || (( $DISPLAY_NUM > 10 ))
    do
     Xvfb :$DISPLAY_NUM &
     jobs
     sleep 2  # assumption here is that Xvfb will exit quickly if it can't launch
     if jobs | grep Xvfb
     then  
       echo launching test on :$DISPLAY_NUM
       xterm -display :$DISPLAY_NUM
       TEST_HAS_RUN=1
       kill %-
     else   
       let DISPLAY_NUM=$DISPLAY_NUM+1
     fi
    done
    

提交回复
热议问题