How to Close Xvfb after usage How to Close Xvfb after usage selenium selenium

How to Close Xvfb after usage


The simple solution is to keep the old value of DISPLAY, change it to point to the xvfb, and then after the test is run you can change it back to the saved value.

This leaves the xvfb running, so you should also get the pid of that process and kill it. Otherwise, if you run this set of steps a dozen times, you'll have a dozen xvfb processes running in the background.


the & on the end of your 2nd command tells Linux to run it command in background. You can see the list of background commands running with $ jobs. Sample on Ubuntu16.04:

$ sudo Xvfb :10 -ac &[1] 31294$ jobs -l[1]+ 31294 Running                 sudo Xvfb :10 -ac &

As you can see on the output above, jobs -l shows the background job and the second column of the output is the PID that can be used to kill it as $ sudo kill 31294.

Another option that may be "cleaner" is to start Xvfb just to run the command you want and quit automatic instead of keep it running in background. This way you would replace your lines 2,3 and 4 by:

xvfb-run python my_selenium_tests.py

Just replace python my_selenium_tests.py with whatever way to run your tests. It will open Xvfb just to it and close at the end.