Skip to content

Instantly share code, notes, and snippets.

@adnan360
Last active January 2, 2019 10:48
Show Gist options
  • Select an option

  • Save adnan360/d177ce12f44cc00ffd42536451ec4622 to your computer and use it in GitHub Desktop.

Select an option

Save adnan360/d177ce12f44cc00ffd42536451ec4622 to your computer and use it in GitHub Desktop.
Start or stop Apache server + Mysql server on different distros and setups
#!/bin/bash
# Arch Linux and Debian has different names for Apache service.
# Some people may like Mysql over Mariadb, which has different service
# names.
# This script detects which service is available and starts or stops it.
# So it basically lets you start or stop the server in one command.
# This is great for maintaining one piece of code that can be used on
# different distros.
# To use it, place the code below on your ~/.bashrc or if you have it,
# on your ~/.bash_aliases, then run:
# source ~/.bashrc or source ~/.bash_aliases
# then use `start-server` or `stop-server` as you need.
handleServer() {
services=$(sudo systemctl list-unit-files)
if echo $services | grep -q apache2; then
sudo systemctl ${1} apache2
fi
if echo $services | grep -q httpd; then
sudo systemctl ${1} httpd
fi
if echo $services | grep -q mariadb; then
sudo systemctl ${1} mariadb
fi
if echo $services | grep -q mysql; then
sudo systemctl ${1} mysql
fi
}
alias start-server='handleServer start'
alias stop-server='handleServer stop'
alias restart-server='handleServer restart'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment