启动服务的脚本
#!/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