How to Close Xvfb after usage
Asked Answered
M

2

10

I am running some tests in headless firefox using Xvfb. However, after my tests are finished I want to move back to normal display. But I am not able to do that .Here is What I am doing .

  1. Open A Terminal
  2. sudo Xvfb :10 -ac &
  3. export DISPLAY=:10
  4. Execute My Tests using RobotFramework+ Selenium

After step 4, I want to open the firefox in the same terminal but I am not able to see it as it is directed towards :10 display.

I wonder how can I shut this (xvfb :10) down so that I can open firefox and view it.

Myrmecology answered 21/7, 2014 at 16:17 Comment(3)
have you tried saving the value of DISPLAY before your test, and then setting it back to that value after the test?Saritasarkaria
Worked like charm, apparently I stored the default value of DISPLAY , set it to new value, ran tests and then as suggested by you, set the DISPLAY to default again.Myrmecology
The answer given by vmenezes to use xvfb-run is better. It will export the DISPLAY variable and automatically clean-up after completion. 1. create test script 2. chmod a+x script 3. xvfb-run -s "-ac" ./test_script 4. open firefoxReunionist
S
4

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.

Saritasarkaria answered 21/7, 2014 at 21:32 Comment(1)
Having to cleanup after executing xvfb is problematic because you (1) you have to remember to do it and (2) you have to make sure that the launched process has completed. I think vmenezes answer should be marked as the accepted answer because it solves both of these issues.Reunionist
N
7

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.

Nelsonnema answered 19/3, 2018 at 21:17 Comment(1)
xvfb-run is by far the best option -- it runs the command and automatically cleanups after the command terminates. However, if you need to run multiple commands using this framebuffer you may want to create a script and use xvfb-run to execute it -- it will automatically export the DISPLAY variable for the sub process/shell.Reunionist
S
4

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.

Saritasarkaria answered 21/7, 2014 at 21:32 Comment(1)
Having to cleanup after executing xvfb is problematic because you (1) you have to remember to do it and (2) you have to make sure that the launched process has completed. I think vmenezes answer should be marked as the accepted answer because it solves both of these issues.Reunionist

© 2022 - 2024 — McMap. All rights reserved.