I have a set of scripts that works on my laptop that works fine as long as I'm booted in to an X session, but if I'm running on a headless server install I get loads of critical fails from Gtk. I'm sure the solution is to use xvfb, but I can't find a working example.

Assistance appreciated.


I followed this article for running a Firefox-based test framework on a server earlier this year. The Xvfb-stuff is pretty simple, we just need to make sure to turn the access controls off so X doesn't get all uppity. Something like this should get you going:

sudo apt-get install xvfb
sudo Xvfb :10 -ac
export DISPLAY=:10
# run your gubbins

Though you should note that this isn't going to provide everything a real desktop session has. If you ever get into that situation (where you need a real desktop) it might be worth separating that out into a virtual machine on top of the server. A little less inefficient but miles more portable.

Edit from Abraham:
I wound up using the methodology, but I wanted it in a python management task. My implementation did the same thing, but it looked more like this:

from django.core.management.base import BaseCommand, CommandError
import subprocess, os
from multiprocessing import Process

def run():
    sub = Process(target=subprocess.call, args=(('Xvfb', ':10', '-ac',), ))
    env = os.environ.copy()
    env['DISPLAY'] = ":10"
    tasks = ('random','management','commands','thatneedDISPLAY:X')
    for task in tasks:
        p = subprocess.call(['python','manage.py',task],env=env)

class Command(BaseCommand):
    def __init__(self):

    args = 'none'
    help = '''Run all daily tasks.'''

    def handle(self, *args, **options):
  • Thanks @Oli. You're a lifesaver. Let me try it out before I accept to see if it works in my env. – RobotHumans Nov 24 '13 at 18:07
  • Also, if you just need to run a single script, xvfb-run -a script will run the script in a session, without having to manually specify a DISPLAY, or hope you pick one that doesn't conflict with existing displays (in case you need to run multiple things under xvfb). – dobey Nov 24 '13 at 18:14
  • I'm running django management commands with subshell inception, so it's not exactly straightforward. I was more hoping for import python-beast-mode-xvfb; python-beast-mode-xvfb.instance(webkit instance) or something, but it's fine. – RobotHumans Nov 24 '13 at 18:30
  • @hbdgaf How so? Terminate doesn't kill-off descendant processes so it might be that X is forking out and being orphaned (but alive). docs.python.org/dev/library/… – Oli Nov 28 '13 at 11:48
  • Nevermind. It worked. I killed a run mid-run to troubleshoot something. When you start naked and let it run xvfb works when run this way. – RobotHumans Nov 28 '13 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.