Find a free X11 display number

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

    Why not exploit the fact that every X11 server puts a "Lock" file into /tmp?

    This is called /tmp/.Xn-lock where "n" is the Display id. (Also note the leading . in the filename).

    This is the mechanism that Xserver itself uses to check for duplication, and it appears to be consistent on all *nix platforms I have tried (HP-UX, Linux, ...)

    So you could adapt your script thus (forgive me for syntax errors, I'm nore accustomed to C shell than Bourne/Korn shell scripting)

    DISPLAY_NUM=0
    
    do
    
      if ( -e /tmp/.X$DISPLAY_NUM-lock ) then
    
         let DISPLAY_NUM=$DISPLAY_NUM+1
    
      else
    
         Xvfb :$DISPLAY_NUM -screen 0 1280x1024x24 -ac   (or whatever args take your fancy)
    
      fi
    
    done
    

提交回复
热议问题