Skip to content

Instantly share code, notes, and snippets.

@yonglai
Created May 22, 2017 21:59
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 yonglai/5ee73d261dcef10c04c925f6e676e414 to your computer and use it in GitHub Desktop.
Save yonglai/5ee73d261dcef10c04c925f6e676e414 to your computer and use it in GitHub Desktop.
script to set up headless selenium in Ubuntu
#!/bin/sh
set -e
if [ -e /.installed ]; then
echo 'Already installed.'
else
echo ''
echo 'INSTALLING'
echo '----------'
# Add Google public key to apt
wget -q -O - "https://dl-ssl.google.com/linux/linux_signing_key.pub" | sudo apt-key add -
# Add Google to the apt-get source list
echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list
# Update app-get
apt-get update
# Install Java, Chrome, Xvfb, and unzip
add-apt-repository ppa:webupd8team/java -y
apt-get update -y
echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | debconf-set-selections
apt-get install -y oracle-java8-installer
apt-get -y install google-chrome-stable xvfb unzip
# Download and copy the ChromeDriver to /usr/local/bin
cd /tmp
wget -N http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip
wget http://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin
mv selenium-server-standalone-3.4.0.jar /usr/local/bin
# So that running `vagrant provision` doesn't redownload everything
touch /.installed
fi
echo Create xvfb startup script
touch /etc/systemd/system/xvfb.service
chmod 644 /etc/systemd/system/xvfb.service
cat <<EOT >> /etc/systemd/system/xvfb.service
[Unit]
Description=Xvfb
After=syslog.target network.target
[Service]
Type=oneshot
RemainAfterExit=yes
User=vagrant
ExecStart=/bin/xvfb.sh
Environment=DISPLAY=:10
[Install]
WantedBy=multi-user.target
EOT
touch /bin/xvfb.sh
chown :vagrant /bin/xvfb.sh
chmod 750 /bin/xvfb.sh
cat <<EOT >> /bin/xvfb.sh
#! /bin/bash
Xvfb :10 -screen 0 1366x768x24 -ac &
EOT
echo Create google-chrome startup script
touch /etc/systemd/system/google-chrome.service
chmod 644 /etc/systemd/system/google-chrome.service
cat <<EOT >> /etc/systemd/system/google-chrome.service
[Unit]
Description=Google Chrome
After=xvfb.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/google-chrome.sh
Environment=DISPLAY=:10
[Install]
WantedBy=multi-user.target
EOT
touch /bin/google-chrome.sh
chmod 750 /bin/google-chrome.sh
cat <<EOT >> /bin/google-chrome.sh
#! /bin/bash
google-chrome --remote-debugging-port=92222 &
EOT
echo Create selenium-server startup script
touch /etc/systemd/system/selenium-server.service
chmod 644 /etc/systemd/system/selenium-server.service
cat <<EOT >> /etc/systemd/system/selenium-server.service
[Unit]
Description=Selenium Standalone Server
After=google-chrome
[Service]
Type=oneshot
RemainAfterExit=yes
User=vagrant
ExecStart=/bin/selenium-server.sh
[Install]
WantedBy=multi-user.target
EOT
touch /bin/selenium-server.sh
chown :vagrant /bin/selenium-server.sh
chmod 750 /bin/selenium-server.sh
cat <<EOT >> /bin/selenium-server.sh
#! /bin/bash
export DISPLAY=:10
cd /usr/local/bin
nohup java -jar ./selenium-server-standalone-3.4.0.jar &
EOT
systemctl daemon-reload
systemctl enable xvfb
systemctl enable google-chrome
systemctl enable selenium-server
systemctl start xvfb
systemctl start google-chrome
systemctl start selenium-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment