1. 程式人生 > 其它 >啟動服務的指令碼

啟動服務的指令碼

#!/bin/bash
case $1 in
start)
systemctl start $2
;;
stop)
systemctl stop $2
;;
restart)
systemctl restart $2
;;
all_restart)
LIST="sshd nginx"
for i in $LIST
do
systemctl restart $i;
done
;;
all_stop)
TEST="sshd nginx"
for i in $TEST
do
systemctl stop $i;
done
;;


*)
echo "Usage: $0 {start|stop|restart}"
esac
if [ $? == 0 ]; then
echo "======命令執行成功====="
else
echo "======命令執行失敗====="
fi
echo ======正在檢查程序======
sleep 0.5
if [ $1 == "all_restart" ]; then
NEM=$(ps -ef | grep $i | grep -vc grep)
if [ $NEM -gt 1 ]; then
echo "$LIST 啟動成功"
else
echo "$LIST 啟動失敗"
fi
elif [ $1 == "all_stop" ]; then
NAM=$(ps -ef | grep $i | grep -vc grep)
if [ $NAM -lt 1 ]; then
echo "$TEST 停止服務成功"
else
echo "$TEST 停止服務失敗"
fi
else
NUM=$(ps -ef | grep $2 | grep -vc grep)
if [ $NUM -gt 1 ]; then
echo "$2 啟動成功"
else
echo "$2 啟動失敗"
fi
fi