I want to get screenshots of a webpage in Python. For this I am using http://github.com/AdamN/python-webkit2png/ .
newArgs = ["xvfb-run", "--server-args=-screen 0, 640x480x24", sys.argv[0]]
for i in range(1, len(sys.argv)):
if sys.argv[i] not in ["-x", "--xvfb"]:
newArgs.append(sys.argv[i])
logging.debug("Executing %s" % " ".join(newArgs))
os.execvp(newArgs[0], newArgs)
Basically calls xvfb-run with the correct args. But man xvfb
says:
Note that the demo X clients used in the above examples will not exit on their own, so they will have to be killed before xvfb-run will exit.
So that means that this script will <????> if this whole thing is in a loop, (To get multiple screenshots) unless the X server is killed. How can I do that?
The documentation for
os.execvp
states:So after calling
os.execvp
no other statement in the program will be executed. You may want to usesubprocess.Popen
instead:Using
subprocess.Popen
, the code to runxlogo
in the virtual framebuffer X server becomes:Now the problem is that
xvfb-run
launchesXvfb
in a background process. Callingprocess.kill()
will not killXvfb
(at least not on my machine...). I have been fiddling around with this a bit, and so far the only thing that works for me is:So this code reads the process ID of
Xvfb
from/tmp/.X99-lock
and sends the process an interrupt. It works, but does yield an error message every now and then (I suppose you can ignore it, though). Hopefully somebody else can provide a more elegant solution. Cheers.