Skip to content

Instantly share code, notes, and snippets.

@shopglobal

shopglobal/selenium

Forked from adeubank/selenium
Created Dec 21, 2017
Embed
What would you like to do?
Set up selenium on Ubuntu 16.04 as a service
#!/bin/bash
case "${1:-''}" in
'start')
if test -f /tmp/selenium.pid
then
echo "Selenium is already running."
else
export DISPLAY=localhost:99.0
java -Dwebdriver.gecko.driver="/usr/lib/geckodriver/geckodriver" -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4444 > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Starting Selenium..."
error=$?
if test $error -gt 0
then
echo "${bon}Error $error! Couldn't start Selenium!${boff}"
fi
fi
;;
'stop')
if test -f /tmp/selenium.pid
then
echo "Stopping Selenium..."
PID=`cat /tmp/selenium.pid`
kill -3 $PID
if kill -9 $PID ;
then
sleep 2
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
else
echo "Selenium could not be stopped..."
fi
else
echo "Selenium is not running."
fi
;;
'restart')
if test -f /tmp/selenium.pid
then
kill -HUP `cat /tmp/selenium.pid`
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
sleep 1
export DISPLAY=localhost:99.0
java -Dwebdriver.gecko.driver="/usr/lib/geckodriver/geckodriver" -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4444 > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Reload Selenium..."
else
echo "Selenium isn't running..."
fi
;;
*) # no parameter specified
echo "Usage: $SELF start|stop|restart"
exit 1
;;
esac
#!/bin/sh
apt-get -qq update
apt-get -qq install -y default-jre firefox xvfb
# install gecko driver
mkdir /usr/lib/geckodriver
cd /usr/lib/geckodriver
wget https://github.com/mozilla/geckodriver/releases/download/v0.13.0/geckodriver-v0.13.0-linux64.tar.gz
tar -xzf geckodriver-v0.13.0-linux64.tar.gz
# setup xvfb to auto start
sed -i -e '$i \Xvfb :99 -ac -screen 0 1024x768x8 > /tmp/xvfb.log 2>&1 &\n' /etc/rc.local
# install selenium
mkdir /usr/lib/selenium
cd /usr/lib/selenium
curl -sO http://selenium-release.storage.googleapis.com/3.0/selenium-server-standalone-3.0.1.jar
ln -s selenium-server-standalone-3.0.1.jar selenium-server-standalone.jar
chmod a+x selenium-server-standalone.jar
mkdir -p /var/log/selenium
chmod a+w /var/log/selenium
curl -s https://gist.githubusercontent.com/adeubank/2d92d66caff2727ce1cc/raw/685c4eb859cc0f7505015e4c6fc816f5adb16759/selenium > /etc/init.d/selenium
chmod 755 /etc/init.d/selenium
update-rc.d selenium defaults
# start xvfb
Xvfb :99 -ac -screen 0 1024x768x8 > /tmp/xvfb.log 2>&1 &
# start selenium
service selenium start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment