查找免费的 X11 显示器编号 [英] Find a free X11 display number
问题描述
我有一些需要 X11 显示器的单元测试,所以我计划在运行它们之前启动 Xvfb,但是要启动 Xvfb,我需要一个免费的显示器编号来连接它.我最好的猜测是看看 /tmp/.X11-unix
中有什么是免费的,但如果许多测试尝试同时开始,我不确定如何处理竞争.
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 is to see what's free in /tmp/.X11-unix
but I'm not sure how to handle the race if many tests try to start simultaneously.
sshd 必须这样做,有人知道怎么做吗?
sshd must do this, does anyone know how?
推荐答案
完全归功于这个答案给相关的问题 X11 显示数字有多高?:
With full credit to this answer to the related question How high do X11 display numbers go?:
最新的 X 服务器从 1.13 版开始(Xvfb
,也是)支持 -displayfd
命令行选项:它将使 X 服务器选择显示并将显示编号写回文件描述符
.这有点令人费解,但这将是一种安全且无竞争条件的方式来要求 Xvfb
使用任何免费显示.bash
示例:
Recent X servers as of version 1.13 (Xvfb
, too) support the -displayfd <fd>
command line option: It will make the X server choose the display itself and write the display number back to the file descriptor <fd>
. It's a bit convoluted, but this would be a safe and race-condition-free way to ask Xvfb
to use any free display. A bash
example:
exec 6>display.log
Xvfb -displayfd 6
# The display number of the new Xvfb instance has been written to display.log
# Kill Xvfb
exec 6>&-
这篇关于查找免费的 X11 显示器编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!