Skip to content

Instantly share code, notes, and snippets.

@LamberKeep
Last active September 25, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LamberKeep/8a0f7a436bd92763f63e63328cea3d25 to your computer and use it in GitHub Desktop.
Save LamberKeep/8a0f7a436bd92763f63e63328cea3d25 to your computer and use it in GitHub Desktop.
Servers Controller CLI
#!/bin/sh
# Configurations.
serviceDirectory="/usr/lib/systemd/system"
# Constants (do not touch).
menuServiceOptions="return status start stop restart enable disable exit"
menuGlobalOptions=$(ls $serviceDirectory)
# Functions.
# Main menu.
# Contains all services in folder to choose.
menuGlobal()
{
echo -e "\nServer controller CLI\n"
i=0
for file in $menuGlobalOptions
do
i=$((i + 1))
name=$(basename $file)
echo -e " $i\t$name"
done
echo
read -p "Choose server: " answer
if ! validateInput $answer $i
then
echo "Not a option."
exit 1
fi
clear
menuService $(echo $menuGlobalOptions | cut -d ' ' -f $answer)
}
# Service menu.
# Contains service control.
menuService()
{
while true
do
echo -e "\n$1\n"
i=0
for option in $menuServiceOptions
do
i=$((i + 1))
echo -e " $i\t$option"
done
echo
read -p "Choose option: " answer
optionChoosen=$(echo $menuServiceOptions | cut -d ' ' -f $answer)
if ! validateInput $answer $i
then
echo "Not a option."
exit 1
fi
clear
case $answer in
1)
menuGlobal
;;
2)
getServiceStatus $1
;;
8)
exit 0
;;
*)
eval "systemctl $optionChoosen $1"
;;
esac
done
}
#
# Prints service status.
#
# Parameters:
# 1 - input service
#
getServiceStatus()
{
#status=$(systemctl status $1)
echo
systemctl status $1
#echo Active: $(echo $status | grep "Active" | cut -d ' ' -f 7)
#echo
}
validateInput()
{
if [ "$1" -ne "$1" -o "$1" -le "0" -o "$1" -gt "$2" ]
then
return 1
fi
return 0
}
clear
menuGlobal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment