#!/bin/bash
###设置死循环,只有当用户输入退出时,才能跳出循环结束进程###
while true
do
###交互显示内容###
echo -e "
\033[31m start 启动服务 \033[0m
\033[32m stop 关闭服务 \033[0m
\033[33m status 服务状态 \033[0m
\033[33m restart 重启服务 \033[0m
\033[34m enable 开机启动 \033[0m
\033[35m disable 开机不启动 \033[0m
\033[36m Q 退出监控 \033[0m
"
###提示用户输入,并接收用户输入值###
read -p "请输入需要设置的选项:" i
case $i in
###输入start
start)
###判断服务先前是否已经启动
if [ $(systemctl status httpd | grep "Active" | awk '{print $2}') == "active" ];then
echo "服务已经启动!"
else
systemctl start httpd &> /dev/null
echo "服务启动成功!"
fi
;;
stop)
systemctl stop httpd &> /dev/null
echo "服务关闭!"
;;
restart)
systemctl restart httpd &> /dev/null
echo "服务重启......"
###判断服务是否重启成功
if [ $(systemctl status httpd | grep "Active" | awk '{print $2}') == "active" ];then
echo "服务重启成功!"
else
echo "服务重启失败!"
fi
;;
status)
###判断服务状态
if [ $(systemctl status httpd | grep "Active" | awk '{print $2}') == "active" ];then
echo "服务处于运行状态!"
elif [ $(systemctl status httpd | grep "Active" | awk '{print $2}') == "inactive" ];then
echo "服务处于关闭状态!"
else
echo "服务出现错误!"
fi
###判断服务是否开机自启动
if [ $(systemctl status httpd|grep Loaded|awk '{print $4}'|awk -F ")" '{print $1}') == "enabled" ];then
echo "服务开机自启动!"
else
echo "服务开机不启动!"
fi
;;
enable)
systemctl enable httpd &> /dev/null
echo "设置服务开机自启动成功!"
;;
disable)
systemctl disable httpd &> /dev/null
echo "设置服务开机不启动成功!"
;;
Q|q)
echo "Bye~"
###退出
exit 0
;;
esac
done