Skip to content

Instantly share code, notes, and snippets.

@alanmsmxyz
Created September 3, 2020 07:55
Show Gist options
  • Save alanmsmxyz/df3acc481cd5e7f91e9c142ea1f28d1b to your computer and use it in GitHub Desktop.
Save alanmsmxyz/df3acc481cd5e7f91e9c142ea1f28d1b to your computer and use it in GitHub Desktop.
Bash script to start / stop LEMP services (Nginx, MySQL, and PHP-FPM) via systemd.
#!/bin/bash
# Bash script to start / stop LEMP services (Nginx, MySQL, and PHP-FPM) via systemd.
# Usage lemp.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 lemp.sh [start|stop|restart]"
exit
fi
# Stopping services
if [ 'stop' = $1 ] || [ 'restart' = $1 ]; then
echo "=== Stopping LEMP ==="
# nginx
if ! pgrep -x nginx > /dev/null; then
echo "nginx is not running, skipping..."
else
echo "Stopping nginx..."
systemctl stop nginx
fi
# php-fpm
if ! pgrep php-fpm > /dev/null; then
echo "php-fpm is not running, skipping..."
else
echo "Stopping php-fpm"
systemctl stop php7.4-fpm
fi
# mysql
if ! pgrep -x mysqld > /dev/null; then
echo "mysql is not running, skipping..."
else
echo "Stopping mysql"
systemctl stop mysql
fi
fi
# Starting services
if [ 'start' = $1 ] || [ 'restart' = $1 ]; then
echo "=== Starting LEMP ==="
# nginx
if pgrep -x nginx > /dev/null; then
echo "nginx already started, skipping..."
else
echo "Starting nginx..."
systemctl start nginx
fi
# php-fpm
if pgrep php-fpm > /dev/null; then
echo "php-fpm already started, skipping..."
else
echo "Starting php7.4-fpm"
systemctl start php7.4-fpm
fi
# mysql
if pgrep -x mysqld > /dev/null; then
echo "mysql already started, skipping..."
else
echo "Starting mysql"
systemctl start mysql
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment