27

I'm using Amazon Linux. I want to run a headless chromium browser to use on my node.js Selnium tests. So I fire up my Xvfb server like so ...

if ! pidof /usr/bin/Xvfb; then Xvfb :0 -screen 5 1024x768x8 & fi

However, when I try and take a screenshot after my tests have failed,

DISPLAY=:0 import -window root /tmp/screenshot.png

I get the error mentioned below...

+ DISPLAY=:0
+ import -window root /tmp/screenshot.png
import: unable to open X server `:0' @ error/import.c/ImportImageCommand/369.

How do I take a screenshot of Xvfb buffer?

5
  • 2
    Your example works fine here (debian 9). You can try xwd and xwud instead of image, works here, too. Maybe check again whether Xvfb is still running, and running on :0? Commented May 15, 2017 at 23:48
  • Do you hvea an example of what you're talking about -- e.g. using another tool besides imagemagick? Commented May 16, 2017 at 13:42
  • 3
    it is similar to your example. xwd -display :0 -root -out /tmp/pic takes a snapshot, and xwud -in /tmp/pic shows it. Commented May 16, 2017 at 15:02
  • Wrks on RHEL/CentOS 7 too. @mviereck you must write an answer. Commented Apr 19, 2019 at 11:50
  • I see an error says unable to open X server, it should be a problem that the X server itself is not reachable Commented Apr 23, 2019 at 11:21

1 Answer 1

13

I tried something similar (on another distribution and to take a screenshot of an xterm window, but this should not be much different) I had to use display :1 as display :0 is already used :

$ Xvfb :1 -screen 5 1024x768x8 &
[1] 23728
$ pidof /usr/bin/Xvfb
23728
$ DISPLAY=:1 xterm 2>/dev/null &
[2] 23767
$ DISPLAY=:1 xwd -root -silent | convert xwd:- png:/tmp/screenshot.png
$

And I have a "/tmp/screenshot.png" file with the expected xterm window on a black background. You can also use

xwd -display :1 -root -silent | convert xwd:- png:/tmp/screenshot.png
2
  • 4
    Debian/Ubuntu: To install xwd and convert, run: apt-get install x11-apps imagemagick Commented Jun 22, 2021 at 23:45
  • Command to run in nixos container: nix-shell -p imagemagick xorg.xwd --run 'DISPLAY=:0 xwd -root -silent | magick xwd:- png:/tmp/screenshot.png' Commented Mar 16 at 13:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.