bash 如何正确终止 xvfb-run

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37481482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:41:32  来源:igfitidea点击:

Howto terminate xvfb-run properly

bashxvfb

提问by dpr

In order to perform some JavaScript unit tests with karmainside a docker container (based on ubuntu 14.04) I'm starting firefox in the container using a karma-script-launcherwith xvfb-run. The start script looks like this:

为了执行与一些JavaScript单元测试因缘泊坞窗容器内(基于Ubuntu 14.04),我使用的是火狐开始在容器卡玛-脚本启动xvfb-run。启动脚本如下所示:

#!/bin/bash
set -o errexit 

# nasty workaround as xvfb-run doesn't cleanup properly...
trap "pkill -f /usr/lib/firefox/firefox" EXIT

xvfb-run --auto-servernum --server-args='-screen 0, 1024x768x16' firefox 

Starting the browser and executing the unit tests works very well. After executing the tests karma terminates the spawned browser instance - in my case the script that launched firefox over xvfb-run.

启动浏览器并执行单元测试效果很好。执行测试后 karma 终止了生成的浏览器实例 - 在我的例子中是通过 xvfb-run 启动 firefox 的脚本。

In the above script you can see that I registered a trapto kill the launched firefox on exit of my script. This works, but the script is not a very nice citizen as it terminates allinstances of firefox that are currently running instead of just terminating the one instance that was launched by the script. I first tried to kill the xfvb-runprocess but killing this process has no effect on the sub-process launched by the xvfb-runscript...

在上面的脚本中,您可以看到我trap在退出脚本时注册了一个来杀死启动的 Firefox。这是有效的,但该脚本不是一个很好的公民,因为它终止当前正在运行的所有Firefox 实例,而不是仅仅终止由脚本启动的一个实例。我首先尝试杀死该xfvb-run进程但杀死该进程对xvfb-run脚本启动的子进程没有影响......

If I start firefox over xvfb-runmanually there is a bunch of spawned processes:

如果我xvfb-run手动启动 firefox,则会有一堆衍生的进程:

root@1d7a5988e521:/data# xvfb-run --auto-servernum --server-args='-screen 0, 1024x768x16' firefox &
[1] 348
root@1d7a5988e521:/data# ps ax
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:00 bash
  348 ?        S      0:00 /bin/sh /usr/bin/xvfb-run --auto-servernum --server-args=-screen 0, 1024x768x16 firefox
  360 ?        S      0:00 Xvfb :99 -screen 0, 1024x768x16 -nolisten tcp -auth /tmp/xvfb-run.bgMEuq/Xauthority
  361 ?        Sl     0:00 /usr/lib/firefox/firefox
  378 ?        S      0:00 dbus-launch --autolaunch bcf665e095759bae9fc1929b57455cad --binary-syntax --close-stderr
  379 ?        Ss     0:00 //bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
  388 ?        S      0:00 /usr/lib/x86_64-linux-gnu/gconf/gconfd-2
  414 ?        R+     0:00 ps ax
root@1d7a5988e521:/data#

If I now kill the xvfb-runprocess (PID 348), only this process will be terminated, leaving the other processes running. If I kill the firefox process (PID 361) instead, the xvfb-runscript correctly terminates and kills the other processes as well. But from my script I only know the PID of the xvfb-runprocess...

如果我现在xvfb-run终止进程 (PID 348),则只会终止该进程,而让其他进程继续运行。如果我改为xvfb-run终止firefox 进程 (PID 361),脚本会正确终止并终止其他进程。但是从我的脚本中我只知道xvfb-run进程的 PID ......

During my research I stumbled across this rather old bug reportfor xvfb-runwhich still seems to be valid in spite of the bug's status beeing fixed back in 2012.

在我的研究,我偶然发现这个相当老的bug报告xvfb-run这似乎仍然是尽管错误的状态在2012年beeing固定靠背有效。

Is there any polite way to terminate the xvfb-runprocess in order for the other processes to be cleaned up correctly?

是否有任何礼貌的方式终止xvfb-run进程以便正确清理其他进程?

采纳答案by dpr

I posted this question on unix.stackexchange.com some time ago as this is more related to Unix/Linux than to programming in general and did not attract much attention here:

我前段时间在 unix.stackexchange.com 上发布了这个问题,因为这与 Unix/Linux 的关系比一般的编程更相关,并且在这里没有引起太多关注:

Howto terminate xvfb-run properly @ Unix & Linux

如何在 Unix 和 Linux 上正确终止 xvfb-run

However the only option to terminate the X-programs correclty seems to be the not use xvfb-run and to write your own script to start the processes with Xvfb.

然而,终止 X 程序正确性的唯一选择似乎是不使用 xvfb-run 并编写自己的脚本以使用 Xvfb 启动进程。