Running Selenium server as a CentOS systemd service

… is very easy to do.

You need two things.  1) Xvfb for the virtual screen, 2) systemd.  (And the Selenium Server binary, of course)

First Xvfb

[Unit]
Description=X Virtual Frame Buffer Service
After=network.target

[Service]
Type=simple
User=magiumlib
ExecStart=/usr/bin/Xvfb :99 -screen 0 1024x768x24

[Install]
WantedBy=selenium.target

This will start up the virtual framebuffer so Selenium has a virtual screen to run the browser on.  Next up is the Selenium service itself.

[Unit]
Description=Selenium
After=xvfb.target

[Service] Type=simple User=magiumlib PIDFile=/var/spool/selenium/pid/master.pid EnvironmentFile=-/etc/sysconfig/network Environment=DISPLAY=:99 Environment=DBUS_SESSION_BUS_ADDRESS=/dev/null ExecStart=/bin/java -Dwebdriver.chrome.driver=/var/www/magiumlib/bin/chromedriver -Dwebdriver.chrome.logfile=/home/magiumlib/chrome.log -Dselenium.LOGGER=/home/magiumlib/selenium.log -jar /var/www/magiumlib/bin/selenium-server-standalone-2.53.0.jar ExecStop=kill `cat /var/spool/selenium/pid/master.pid` [Install] WantedBy=multi-user.target

Voila!  Automated Selenium.