How do I start Xvfb on startup on Debian?

On debian, you can add the Xvfb command to /etc/rc.local. If Xvfb doesn't detach itself into the background, use an & on the end of the command.

Using cron would work too, although init scripts give you more control over exactly when in the startup sequence it will run, and are the traditional way of starting services.


As @Gilles suggested, it's more suitable to run on demand, especially when you're using it for Selenium testing. Here is example shell commands:

export DISPLAY=:99
xdpyinfo -display $DISPLAY > /dev/null || Xvfb $DISPLAY -screen 0 1024x768x16 &

Checking display using xdpyinfo before will make sure you won't run virtual framebuffer twice.


If you really need to start it on startup, you can try the following init.d script:

#!/bin/sh
XVFB=/usr/bin/Xvfb
XVFBARGS=":1 -screen 0 1024x768x24 -ac +extension GLX +render -noreset -nolisten tcp"
PIDFILE=/var/run/xvfb.pid
case "$1" in
  start)
    echo -n "Starting virtual X frame buffer: Xvfb"
    start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    ;;
  stop)
    echo -n "Stopping virtual X frame buffer: Xvfb"
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
    echo "."
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
        echo "Usage: /etc/init.d/xvfb {start|stop|restart}"
        exit 1
esac

exit 0

Source: dloman/xvfb at GitHub (forked from: jterrace/xvfb)

Save it as /etc/init.d/xvfb and make it executable, then start it as:

/etc/init.d/xvfb start

To automatically run on startup, run:

sudo update-rc.d xvfb defaults

To remove it from autorun, run:

sudo update-rc.d -f xvfb remove

Also add to environment file to be recognized by X programs, e.g.:

echo DISPLAY=":1" | sudo tee -a /etc/environment

Here is version for systemd version: dloman/xvfb.service.


Your premise is invalid: there is no reason to run Xvfb as root. Your usual X server only needs to run as root (on many but not all unices) only so that it can access the video hardware; that's not an issue for Xvfb by definition.

If you want to run Xvfb at startup, you can use cron with @reboot replacing the five date-and-time fields. You can do that in any user's crontab, not just root's. But I don't see the point. Your scripts would be more robust if you started Xvfb when you need it, just the way you seem to have originally planned it.