Tuesday 15 September 2015

Running X applications without an X server using Xvfb

Recently at work,  I needed to automate an application which converts files from a certain document format to another.

The only converter I found was a GUI application developed using the QT library.

Luckily, it accepted the input and output file names as command line parameters, so we can automate it from the command line. But unfortunately, it pops up a small window during the file conversion so it needs to run using an X server.

This is not fine at all since the script was meant to be run daily at night, and we could not afford wasting resources by running an X server.

Xvfb comes to the rescue! Xvfb is a display server which performs all graphical operations in memory! No screen/monitor required!

You can launch it as follows:
Xvfb :1 -screen 0 1024x768x16 &
This will create a 1024x768 screen at 16-bits per pixel and the display will be named ":1"
You can then make your application use the display as follows:
myapp -display :1 [input parameters]
Or:
xvfb-run myapp [input parameters]
You can also export the DISPLAY variable in bash, then run your command normally afterwards:
export DISPLAY=:1

myapp
You can even grab a screenshot (using ImageMagick):
import -display :1 -window root image.png
See Xvfb on Wikipedia

1 comment:

  1. For dealing with legacy GUI applications, It helps a lot... without the overhead and security impact of running a full Xorg on your server :) Nice find !



    ReplyDelete