How to run GUI-programs on a server without any monitor
1 Oct2007

This weekend I was doing some development for one of our projects and we needed to make screenshots of a web pages (see my next posts about this task). I’ve managed to develop small piece of code which uses GtkMozEmbed component (Mozilla Gecko-based renderer for web pages) to create screenshots of any page, but there was some problem… The problem was a following: GTK+ library can’t work w/o fully-functional X server running on your machine. Obviously I didn’t want to run such software (no monitor/keyboard/mouse, dumb graphics adapter on the server, etc, etc) so I’ve tried to find some solution… And in this tiny article I’ll describe the method I’ve managed to find.

The solution was pretty simple and its name is Xvfb. Xvfb is an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb frame-buffer using virtual memory. So, first step was obvious (thanks to great Debian distribution):

# apt-get install xvfb

Second step was to run our server for testing:

# Xvfb -shmem -screen 0 1280x1024x24

This command starts virtual X server with virtual display :0 which has virtual resolution of 1280×1024 and 24 bit virtual colors ;-). To test it you can run a following command:

# DISPLAY=:0 xdpyinfo

If everything works fine, you’ll get a lots of status information about your server. As you may be noticed in previous command, I’ve specified X display information before running a command with DISPLAY=:0. You can export this variable one time and then run any commands which require X server to be running.

One more interesting thing here is how to make this virtual server to run all the time and restart in case of any problems. It could be done with simple technique using linux /etc/inittab file. So I’ve added a following line to this file:

xvfb:2:respawn:/usr/bin/Xvfb :0 -ac -screen 0 2048x1536x24

and reloaded it with init q command.