Permalink
Find file
9adf1b1 Sep 3, 2012
executable file 194 lines (174 sloc) 4.99 KB
#!/bin/bash
HEADLESS_SELENIUM_CONFIG='/etc/headless-selenium/selenium.conf'
HEADLESS_SELENIUM_DIR='/usr/lib/headless-selenium'
#Configurable Variables.
SELENIUM_JAR=$HEADLESS_SELENIUM_DIR/selenium-server-standalone-2.24.1.jar
SELENIUM_OPTIONS=''
SELENIUM_PORT=4444
SELENIUM_PROFILE_DIR=$HEADLESS_SELENIUM_DIR/profiles/firefox/selenium
JAVA=`which java`
XVFB=`which Xvfb`
DISPLAY=:42
XVFB_SCREEN_GEOMETRY=1024x768x24
TIME_OUT=300
if [ -f $HEADLESS_SELENIUM_CONFIG ]
then
source $HEADLESS_SELENIUM_CONFIG
fi
# If custom options aren't given.
if [ "$SELENIUM_OPTIONS" == "" ]
then
# Preparing for the ability to load configurable Profile Directories.
if [[ "$SELENIUM_PROFILE_DIR" != "" && -d $SELENIUM_PROFILE_DIR ]]
then
SELENIUM_OPTIONS="$SELENIUM_OPTIONS -firefoxProfileTemplate $SELENIUM_PROFILE_DIR"
else
echo "${bon}Warning:${boff}No Profile in ${bon}$SELENIUM_PROFILE_DIR${boff} detected."
echo "A default firefox profile will be used and may produce unexpected results."
fi
if [ "$SELENIUM_PORT" != "" ]
then
SELENIUM_OPTIONS="$SELENIUM_OPTIONS -port $SELENIUM_PORT"
fi
fi
SELENIUM_LOG_DIR='/var/log/selenium'
XVFB_PID_FILE='/tmp/Xvfb.pid'
SELENIUM_PID_FILE='/tmp/selenium.pid'
case "${1:-''}" in
'start')
# Check for java
if [[ "$JAVA" = "" || ! -x $JAVA ]]
then
echo "${bon}Error Headless Selenium requires java to run!${boff}"
exit 999
fi
# Check for Xvfb
if [[ "$XVFB" = "" || ! -x $XVFB ]]
then
echo "${bon}Error Headless Selenium requires Xvfb to run!${boff}"
exit 998
fi
# Check for Server Jar
if [ ! -e $SELENIUM_JAR ]
then
echo "${bon}Error Headless Selenium requires a selenium server jar file to run!${boff}"
exit 997
fi
# Check to see if Stuff is already running.
if $0 status > /dev/null
then
echo "Headless Selenium is already running."
exit 1
fi
if [ ! -d $SELENIUM_LOG_DIR ]
then
mkdir $SELENIUM_LOG_DIR
fi
echo "Starting Xvfb..."
$XVFB $DISPLAY -screen 0 $XVFB_SCREEN_GEOMETRY >> $SELENIUM_LOG_DIR/Xvfb-output.log 2>> $SELENIUM_LOG_DIR/Xvfb-error.log & echo $! > $XVFB_PID_FILE
error=$?
if [ $error -gt 0 ]
then
echo "${bon}Error $error! Couldn't start Xvfb!${boff}"
exit 2
fi
# Just in case this file is still hanging around.
rm /tmp/selenium-session-output
# The magic line that starts Selenium
(DISPLAY=$DISPLAY $JAVA -jar $SELENIUM_JAR $SELENIUM_OPTIONS 2>>$SELENIUM_LOG_DIR/selenium-error.log | tee /tmp/selenium-session-output >> $SELENIUM_LOG_DIR/selenium-output.log) & echo $! > $SELENIUM_PID_FILE
error=$?
if [ $error -gt 0 ]
then
echo "${bon}Error $error! Couldn't start Selenium!${boff}"
exit 3
fi
num=0
# Had to add time here so that the output file would have time to be created
sleep 2
if [ -e /tmp/selenium-session-output ]
then
echo -n "Starting Selenium"
# Wait for the server to finish starting up
seconds=0
while [[ $num -eq 0 && $seconds -le $TIME_OUT ]]; do
sleep 3
echo -n "."
if netstat -l | grep ":$SELENIUM_PORT " | grep -q -i listen
then
num=1
fi
if egrep -q -i "(fail|error)" /tmp/selenium-session-output
then
num=2
fi
if [ "`cat /tmp/selenium-session-output`" = "" ]
then
num=3
fi
seconds=`expr $seconds + 3`
done
echo ""
else
echo "selenium-session-output doesn't exist"
exit 8
fi
# If we come out of the loop with 2... there's a problem.
if [[ $num -eq 2 || $num -eq 3 ]]
then
echo "${bon}Error $error! Couldn't start Selenium!${boff}"
sleep 1
$0 stop
exit 4
fi
;;
'stop')
if $0 status > /dev/null
then
echo "Stopping Headless Selenium..."
SELENIUM_PID=`cat $SELENIUM_PID_FILE`
XVFB_PID=`cat $XVFB_PID_FILE`
error=0
if pkill -9 -P $SELENIUM_PID > /dev/null && kill -3 $XVFB_PID > /dev/null
then
sleep 2
else
echo "Selenium could not be stopped... There may be orphaned processes lying around."
error=5;
fi
test -f $XVFB_PID_FILE && rm -f $XVFB_PID_FILE
test -f $SELENIUM_PID_FILE && rm -f $SELENIUM_PID_FILE
if [ $error -gt 0 ]
then
exit $error
fi
else
echo "Headless Selenium is not running."
exit 6
fi
;;
'restart')
if $0 status > /dev/null
then
$0 stop
sleep 1
echo "Reload Headless Selenium..."
$0 start
else
echo "Headless Selenium is stopped."
fi
;;
'status')
if test -f $SELENIUM_PID_FILE && test -f $XVFB_PID_FILE && ps -A | grep `cat $SELENIUM_PID_FILE` > /dev/null && ps -A | grep `cat $XVFB_PID_FILE` > /dev/null
then
echo "Headless Selenium is running."
exit 0
else
echo "Headless Selenium is stopped."
exit 1
fi
;;
*) # no parameter specified
echo "Usage: $SELF start|stop|restart|status"
exit 1
;;
esac