Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active October 29, 2019 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/7889622 to your computer and use it in GitHub Desktop.
Save eusonlito/7889622 to your computer and use it in GitHub Desktop.
Based on https://coderwall.com/p/tog9eq but with SCREEN auto detect and some minor fixes
#!/bin/bash
echo ''
XVFB=`which Xvfb`
if [ "$XVFB" == '' ]; then
echo 'Xvfb is not installed'
echo ''
exit 1;
fi
XRANDR=`which xrandr`
if [ "$XRANDR" == '' ]; then
SCREEN=0
else
INFO="$XRANDR -q";
EXPR='Screen ([0-9]+):'
if [[ $INFO =~ $EXPR ]]; then
SCREEN=$((${BASH_REMATCH[${#BASH_REMATCH[*]} - 1]} + 1))
else
SCREEN=0
fi
fi
XVFBARGS=":$SCREEN -screen $SCREEN 1024x768x24 -ac +extension GLX +render -noreset"
PIDFILE=/var/run/xvfb.pid
case "$1" in
start)
echo 'Starting virtual X frame buffer: Xvfb'
start-stop-daemon --chuid www-data --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
;;
stop)
echo 'Stopping virtual X frame buffer: Xvfb'
start-stop-daemon --chuid www-data --stop --quiet --pidfile $PIDFILE
;;
restart)
$0 stop
$0 start
;;
*)
echo 'Usage: '$0' {start|stop|restart}'
echo ''
exit 1
esac
echo ''
exit 0
@jzfgo
Copy link

jzfgo commented Jul 8, 2015

Thanks!

Here is a version for systemd: https://gist.github.com/nkm/91006178753df6f503c1

@robregonm
Copy link

robregonm commented Jun 29, 2017

Line 34 generates an error, it works this way:
start-stop-daemon --chuid www-data --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS

Hope it helps.

@hey-sam
Copy link

hey-sam commented Oct 18, 2019

https://gist.github.com/nkm/91006178753df6f503c1

Thanks for that.
Took me nearly an hour of debugging, before I noticed your comment 😅
Line 18 also causes an error on my ubuntu 14.04 os:

Can't open display

And then actually responds it started xvfb, when it didn't.
Simply used double quotes and it worked fine afterwards: INFO="$XRANDR -q";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment