Skip to content

Instantly share code, notes, and snippets.

@bishwajitcadhikary
Last active September 13, 2024 22:15
Show Gist options
  • Save bishwajitcadhikary/ad36532eb8e5538ac5e7868deda63fe0 to your computer and use it in GitHub Desktop.
Save bishwajitcadhikary/ad36532eb8e5538ac5e7868deda63fe0 to your computer and use it in GitHub Desktop.
selenium-server-linux
#!/bin/bash
# Ensure the script is run with sudo
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root. Please run with sudo."
exit 1
fi
# Ask for the username
read -p "Enter your username for the systemd file: " username
# Install OpenJDK if it's not installed
if ! command -v java &> /dev/null; then
echo "Installing OpenJDK..."
sudo apt-get update
sudo apt-get install -y openjdk-11-jdk
else
echo "OpenJDK is already installed."
fi
# Download Selenium Server JAR
echo "Downloading Selenium Server..."
curl -L https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.24.0/selenium-server-4.24.0.jar -o selenium-server-4.24.0.jar
# Move Selenium Server JAR to /usr/local/lib/
echo "Moving Selenium Server JAR to /usr/local/lib/..."
sudo mv selenium-server-4.24.0.jar /usr/local/lib/
# Create an executable script for Selenium Server
echo "Creating selenium-server executable script in /usr/local/bin/..."
sudo bash -c 'cat > /usr/local/bin/selenium-server << EOF
#!/bin/bash
java -jar /usr/local/lib/selenium-server-4.24.0.jar "\$@"
EOF'
# Set permissions to make the script executable
sudo chmod +x /usr/local/bin/selenium-server
# Create systemd service file
echo "Creating systemd service file for Selenium Server..."
sudo bash -c "cat > /etc/systemd/system/selenium-server.service << EOF
[Unit]
Description=Selenium Server
After=network.target
[Service]
ExecStart=/usr/local/bin/selenium-server standalone --host 0.0.0.0 --port 4444 --session-timeout 999999999
User=$username
Restart=always
[Install]
WantedBy=multi-user.target
EOF"
# Reload systemd daemon and start the service
echo "Reloading systemd and starting Selenium Server service..."
sudo systemctl daemon-reload
sudo systemctl enable selenium-server
sudo systemctl start selenium-server
# Expose the port in the firewall (works for both UFW and firewalld)
echo "Opening port 4444 for external access..."
if command -v ufw &> /dev/null; then
sudo ufw allow 4444
sudo ufw reload
elif command -v firewall-cmd &> /dev/null; then
sudo firewall-cmd --zone=public --add-port=4444/tcp --permanent
sudo firewall-cmd --reload
else
echo "No supported firewall detected. You may need to manually open port 4444."
fi
# Confirm installation success
echo "Selenium Server installed and started successfully!"
echo "You can verify the service with: sudo systemctl status selenium-server"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment