#!/bin/bash
###################################
# #
# OWNER: A white coat a #
#
# TIME.2024/8 #
# #
# version:1.0 #
# #
###################################
#ClearScreen, look better, not much more to say haha
clear
# Declare an array to store data
declare-A services
Sample #array ha
services=(
["Service 1"]="Path where the service is located (to the level where the startup script is located), path where the service startup script is located (contains the startup script), name of the service (the one that is displayed in the process), command to stop the service, path where the print log is located (contains the log file)"
["Service 2"]="Path where the service is located (to the level where the startup script is located), path where the service startup script is located (contains the startup script), service name (the one shown in the process), service stop command, path where the print log is located (contains the log file)"
["Service 3"]="Path to the service (to the level where the startup script is located),Path to the service startup script (which contains the startup script),Service name (the one shown in the process),Service stop command"
# Multiple service messages can be put on the back
)
# Not much more to say on this one haha, just go to the array to get the path to the startup script and call the start command to start the service
start_service() {
# Declare a variable to get the information from the array for later calls.
local service_path=$(echo ${services[$1]} | cut -d',' -f1)
local start_script=$(echo ${services[$1]} | cut -d',' -f2)
echo "Starting $1 at $service_path......"
cd $service_path
$start_script start || { echo "Failed to start $1"; return 1; }
}
# Stop the service module, this part is a bit special, I did a judgment because the stop command of a service is not stop, it is shutdown.
#If you have such a situation, you can make changes in this place, if not, you can take out the if in the middle.
stop_service() {
# Declare a variable to get the information from the array for later calls.
local service_path=$(echo ${services[$1]} | cut -d',' -f1)
local stop_script=$(echo ${services[$1]} | cut -d',' -f2)
local service_name=$(echo ${services[$1]} | cut -d',' -f3)
echo "Stopping $1 with: $service_path......"
cd $service_path
#You can remove this if module if you don't need it.
if [ "$service_name" == "Special services" ];
then
$stop_script shutdown || { echo "Failed to stop $1"; return 1; }
else
$stop_script stop || { echo "Faild to stop $1"; return 1; }
fi
}
# Restart the module, this is much simpler, I just call the stop, start module over and over again.
restart_service() {
local service_name=$1
# If the service is in a normal state, stop it, then start it, otherwise start it directly
if check_service_status $service_name; then
stop_service $service_name
fi
start_service $service_name
}
# Check the status of the service, I use the ps command on my side. I use the ps command to see if the service exists in the process and to determine if the service is alive or not
check_service_status() {
local process_name=$(echo ${services[$1]} | cut -d',' -f3)
if pgrep -u $(whoami) -f "$process_name" > /dev/null; then
echo "$1 is already running."
return 0
else
echo "$1 is not running."
return 1
fi
}
#This piece is also used by special services, there is a service that takes too long to start so it must print the log 。。。。
#If you want all services to print logs, you can remove the judgment below, I'll mark it as
#tail appserver&dbbackup log after them start
watch_log(){
local service_name=$1
local log_file=$(echo ${services[$service_name]} | cut -d',' -f5)
if [ -n "$log_file" ];
then
echo "Now Show log for $service_name: $log_file"
trap " echo 'Now Stop Show log for $service_name'; return" SIGINT
tail -f $log_file
else
echo "No log to show "
fi
}
#list of services for checking service state
service_processes_list() {
for service in "${!services[@]}"; do
echo "Showing the service processes for $service"
check_service_status $service
done
}
# Main menu display, linux scripts are like this, ugly is ugly, just use it!
while true; do
echo "#################################"
echo "#****Services Control Menu******#"
echo "# #"
echo "# 1. Start Server #"
echo "# 2. Stop Server #"
echo "# 3. Restart Server #"
echo "# 4. Check Service status #"
echo "# 5. Exit menu #"
echo "#################################"
read -p "Please select an option: " option
# Go to the actual menu function module huh?
case $option in
1)
while true; do
echo "Select a service to start:"
select service in "${!services[@]}" "Back to main menu"; do
case $service in
"Back to main menu")
break 2
;;
*)
if [ -n "$service" ]; then
start_service $service
sleep 3
#This place is what I mean by special handling, these two special services need to print logs, not just through the process to determine whether the startup normal
# If you want to print the logs of all services, you can remove this piece of judgment.
if [[ "$service" == "Special services 1" || "$service" == "Special services 2" ]];
then
watch_log $service
fi
echo "Press any key to continue..."
read -n 1 -s
break
else
echo "Invalid option, please try again."
fi
;;
esac
done
done
;;
2)
while true; do
echo "Select a service to stop:"
select service in "${!services[@]}" "Back to main menu"; do
case $service in
"Back to main menu")
break 2
;;
*)
if [ -n "$service" ]; then
if check_service_status $service; then
#Before stopping the service, I made a hint to save myself from stopping the production environment by mistake.
read-p "Are you sure want to stop $service ? (Y/N):" confirm
if [ "$confirm" == "Y" ];
then
stop_service $service
sleep 3
fi
else
echo "$service is not running."
fi
echo "Press any key to continue..."
read -n 1 -s
break
else
echo "Invalid option, please try again."
fi
;;
esac
done
done
;;
3)
while true; do
echo "Select a service to restart:"
select service in "${!services[@]}" "Back to main menu"; do
case $service in
"Back to main menu")
break 2
;;
*)
if [ -n "$service" ]; then
restart_service $service
sleep 3
#This place is the same as above, it's just for special services, you can take it off if you don't need it #
if [[ "$service" == "Special services 1" || "$service" == "Special services 2" ]];
then
watch_log $service
fi
echo "Press any key to continue..."
read -n 1 -s
break
else
echo "Invalid option, please try again."
fi
;;
esac
done
done
;;
4)
service_processes_list
sleep 3
echo "Press any key to continue..."
read -n 1 -s
;;
5)
exit 0
;;
*)
echo "Invalid option, please try again."
;;
esac
done