Skip to content

Instantly share code, notes, and snippets.

@chantysothy
Forked from adeubank/selenium
Last active March 31, 2020 07:47
Show Gist options
  • Select an option

  • Save chantysothy/db723e7d55ce60629c6aac46f92a340e to your computer and use it in GitHub Desktop.

Select an option

Save chantysothy/db723e7d55ce60629c6aac46f92a340e to your computer and use it in GitHub Desktop.
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" -Dwebdriver.chrome.driver="/usr/lib/chromedriver/chromedriver" -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4441 > /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" -Dwebdriver.chrome.driver="/usr/lib/chromedriver/chromedriver" -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4441 > /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
# Install Chrome.
wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
sudo apt-get -f install -y
sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
rm ~/google-chrome-stable_current_amd64.deb
mkdir /usr/lib/chromedriver
wget -N https://chromedriver.storage.googleapis.com/2.30/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/lib/chromedriver
sudo chmod +x /usr/lib/chromedriver/chromedriver
# 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 https://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
ln -s selenium-server-standalone-3.4.0.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/chantysothy/db723e7d55ce60629c6aac46f92a340e/raw/f94e62fb257e9220b7934724dc89d6763876a07c/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