title: Howto terminate xvfb-run properly
body: <p>In order to perform some JavaScript unit tests with <a href="https://github.com/karma-runner/karma" rel="noreferrer">karma</a> inside a docker container (based on ubuntu 14.04) I'm starting firefox in the container using a <a href="https://github.com/karma-runner/karma-script-launcher" rel="noreferrer">karma-script-launcher</a> with <code>xvfb-run</code>. The start script looks like this:</p>
  
  <pre><code>#!/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 $1
  </code></pre>
  
  <p>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.</p>
  
  <p>In the above script you can see that I registered a <code>trap</code> to kill the launched firefox on exit of my script. This works, but the script is not a very nice citizen as it terminates <strong>all</strong> instances 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 <code>xfvb-run</code> process but killing this process has no effect on the sub-process launched by the <code>xvfb-run</code> script...</p>
  
  <p>If I start firefox over <code>xvfb-run</code> manually there is a bunch of spawned processes:</p>
  
  <pre><code>root@1d7a5988e521:/data# xvfb-run --auto-servernum --server-args='-screen 0, 1024x768x16' firefox &amp;
  [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#
  </code></pre>
  
  <p>If I now kill the <code>xvfb-run</code> process (PID 348), only this process will be terminated, leaving the other processes running. If I kill the firefox process (PID 361) instead, the <code>xvfb-run</code> script correctly terminates and kills the other processes as well. But from my script I only know the PID of the <code>xvfb-run</code> process...</p>
  
  <p>During my research I stumbled across <a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=626501" rel="noreferrer" title="xvfb-run leaves Xvfb running when killed">this rather old bug report</a> for <code>xvfb-run</code> which still seems to be valid in spite of the bug's status beeing fixed back in 2012.</p>
  
  <p>Is there any polite way to terminate the <code>xvfb-run</code> process in order for the other processes to be cleaned up correctly?</p>