Skip to content

Instantly share code, notes, and snippets.

@mahasak
Last active May 10, 2016 04:00
Show Gist options
  • Save mahasak/e08a1214cade9fb60d470a4d416318b6 to your computer and use it in GitHub Desktop.
Save mahasak/e08a1214cade9fb60d470a4d416318b6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# xvfb - this script starts and stops Xvfb for using with Selenium Grid
#
# chkconfig: 345 95 50
# description: Starts Xvfb on display 99
# processname: Xvfb
# pidfile: /var/log/Xvfb/Xvfb.pid
#
# Place this script as /etc/init.d/xvfb and chmod +x /etc/init.d/xvfb
# Source function library.
. /etc/rc.d/init.d/functions
log_dir=/var/log/Xvfb
error_log=$log_dir/Xvfb_error.log
std_log=$log_dir/Xvfb_std.log
pid_file=$log_dir/Xvfb.pid
xvfb=$( which Xvfb )
user=root
screen_options=":99 -ac -screen 0 1024x768x8"
start() {
if test -f $pid_file
then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null;
then
echo "Xvfb is already running: $PID"
exit 0
else
echo "Removing stale pid file: $pid_file"
fi
fi
echo -n "Starting Xvfb..."
su $user -c "$xvfb $screen_options -nolisten tcp >$std_log 2>$error_log &"
if [ $? == "0" ]; then
success
else
failure
fi
echo
ps -C Xvfb -o pid,cmd | grep Xvfb | awk {'print $1 '} > $pid_file
}
stop() {
if test -f $pid_file
then
echo -n "Stopping Xvfb..."
PID=`cat $pid_file`
su $user -c "kill -15 $PID"
if kill -9 $PID ;
then
sleep 2
test -f $pid_file && rm -f $pid_file
success
else
echo "Xvfb could not be stopped..."
failure
fi
else
echo "Xvfb is not running."
failure
fi
echo
}
status() {
if test -f $pid_file
then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null ;
then
echo "Xvfb is running...$PID"
else
echo "Xvfb isn't running..."
fi
else
echo "Xvfb isn't running..."
fi
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
stop
start
;;
status)
$1
;;
*)
echo "Usage: $SELF start|stop|restart|status"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment