10

I’ve got a Debian web server that I’m using for occasional screen-scraping via Selenum and Python.

I use Xvfb as a virtual X server for Firefox to run on when being controlled by Selenium. Given that I (think) I have to start this using sudo, I don’t think it’ll be feasible to start it from Python each time (see my Stack Overflow question), so I figured I could just have it running all the time.

I currently start it like this at the command line:

sudo Xvfb :99 -nolisten tcp -fbdir /var/run

What method should I use to do this automatically at startup? Should I use cron, as suggested here?

3 Answers 3

6

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.

2
  • Aha, excellent. Is editing /etc/rc.local preferred to using update-rc.d with one’s own individual scripts? Apr 1, 2011 at 21:36
  • 2
    @Paul I would say update-rc.d is preferred and is certainly more capable, but rc.local is quicker and easier. Also, I think Gilles is right, there's no reason this needs to run as root.
    – Andy
    Apr 1, 2011 at 22:31
5

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.

2
  • I know this is a year old but cannot seem to get a javascript selenium version to work with this. If I run after booting it works but when I start it as a service it's like it doesn't detect xvfb. Any idea how I can get both to work as services on start? Apr 14, 2017 at 15:06
  • @MasindeMuliro You can ask another question then, so the answers can be more updated to be specific to your problem.
    – kenorb
    Apr 20, 2017 at 12:55
4

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.

1
  • Valid point. To run Xvfb for one command only you can use the xvfb-run command which usually ships with Xvfb. Here is the cli reference for xvfb-run manpages.debian.org/stretch/xvfb/xvfb-run.1.en.html. @Paul you could run it as follows: xvfb-run --server-args="-nolisten tcp -fbdir /var/run" python some-selenium.py
    – sebisnow
    Jan 30, 2019 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .