Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Created July 3, 2017 14:26
Show Gist options
  • Save molotovbliss/951d4286b66f1ca77030ebd1ab8277a7 to your computer and use it in GitHub Desktop.
Save molotovbliss/951d4286b66f1ca77030ebd1ab8277a7 to your computer and use it in GitHub Desktop.
restart all lamp services
#!/bin/bash
# Restart an array of services for the LAMP stack
# define array of services to restart
services=("php7.0-fpm" "apache2" "mysql" "redis_6379_cache" "redis_6380_fpc" "redis_6381_sessions")
# Define short or long options
SHORT=a
LONG=action
# Detect if not a super user
if [[ $EUID -ne 0 ]]; then
echo "You must be a root/su user" 2>&1
exit 1
fi
# detect if getopt is available or not
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "I’m sorry, `getopt --test` failed in this environment."
exit 1
fi
# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via -- "$@" to separate them correctly
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
h=y
echo "lamp: LAMP services with PHP switching from 5.6 to 7.0"
echo "--------------------------------------------------------------"
echo " --h|help this help"
echo " --a|action service action: options ((re)start,stop,status)"
exit
shift
;;
-a|--action)
a=y
shift
;;
--)
shift
break
;;
*)
echo "error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 1 ]]; then
echo "$0: action required.."
exit 4
fi
echo "-------------------------------------------------------------"
echo "Action: $1 all LAMP Services: ${services[@]}"
echo "-------------------------------------------------------------"
for ii in "${services[@]}"
do
:
echo "service $ii $1 ...";
service $ii $1;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment