Find a free X11 display number

前端 未结 5 1858
旧巷少年郎
旧巷少年郎 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:22

    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
    

提交回复
热议问题