Skip to content

Instantly share code, notes, and snippets.

@alanmsmxyz
Last active September 3, 2020 07:54
Show Gist options
  • Save alanmsmxyz/3b4e4c43612f6f901c7af33b6b65268f to your computer and use it in GitHub Desktop.
Save alanmsmxyz/3b4e4c43612f6f901c7af33b6b65268f to your computer and use it in GitHub Desktop.
Bash script to start / stop LAMP services (Apache, MySQL/MariaDB, and PHP-FPM) via systemd.
#!/bin/bash
# Bash script to start / stop LAMP services (Apache, MySQL/MariaDB, and PHP-FPM) via systemd.
# Usage lamp.sh [start|stop|restart]
# Check if the script run as root
if [ `whoami` != 'root' ]; then
echo "You need to run this as root"
exit
fi
# Check if script called with valid argument
if [ ! -z "$1" ] &&
[ $1 != 'start' ] &&
[ $1 != 'stop' ] &&
[ $1 != 'restart' ]; then
echo "Invalid arguments"
echo "Usage lamp.sh [start|stop|restart]"
exit
fi
# Stopping services
if [ 'stop' = $1 ] || [ 'restart' = $1 ]; then
echo "=== Stopping LAMP ==="
# httpd
if ! pgrep -x httpd > /dev/null; then
echo "httpd is not running, skipping..."
else
echo "Stopping httpd..."
systemctl stop httpd
fi
# php-fpm
if ! pgrep -x php-fpm > /dev/null; then
echo "php-fpm is not running, skipping..."
else
echo "Stopping php-fpm"
systemctl stop php-fpm
fi
# mariadb
if ! pgrep -x mysqld > /dev/null; then
echo "mariadb is not running, skipping..."
else
echo "Stopping mariadb"
systemctl stop mariadb
fi
fi
# Starting services
if [ 'start' = $1 ] || [ 'restart' = $1 ]; then
echo "=== Starting LAMP ==="
# httpd
if pgrep -x httpd > /dev/null; then
echo "httpd already started, skipping..."
else
echo "Starting httpd..."
systemctl start httpd
fi
# php-fpm
if pgrep -x php-fpm > /dev/null; then
echo "php-fpm already started, skipping..."
else
echo "Starting php-fpm"
systemctl start php-fpm
fi
# mariadb
if pgrep -x mysqld > /dev/null; then
echo "mariadb already started, skipping..."
else
echo "Starting mariadb"
systemctl start mariadb
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment