shell 实战 -- 基于一个服务启动,关闭,状态检查的脚本

功能说明:

  • check:检查服务状态,在开启,关闭,状态检查时都会用到这个函数,所以封装起来放到最前面
  • start:开启服务
  • stop:关闭服务
  • fstop:强制关闭
  • status:检查服务状态
  • running:查看所有正在运行的状态

使用说明:

开启服务:

启动一个进程监听 9000(默认)端口:

  • sh standard_server.sh start

启动一个进程监听 8000(自定义)端口:

  • 格式: sh standard_server.sh start ipaddress port
  • sh standard_server.sh start 10.10.4.200 8000

关闭服务:

关闭监听 9000(默认)端口:

  • sh standard_server.sh stop

关闭监听 8000(自定义)端口:

  • 格式: sh standard_server.sh stop ipaddress port
  • sh standard_server.sh stop 10.10.4.200 8000

查看监听状态:

查看 9000 端口(默认)监听状态:

  • sh standard_server.sh status

查看 8000端口(自定义)监听状态:

  • 格式: sh standard_server.sh status ipaddress port
  • sh standard_server.sh status 10.10.4.200 8000

查看所有正在监听端口:

  • sh standard_server.sh running

代码部分:

#!/bin/bash
# author: ck check()
{
if (($(ps aux|grep manage.py| grep "${port:-9000}" |grep -v grep|wc -l) == 0));then
# stopped
return 1;
else
# running
return 0;
fi
} start()
{
check
if (($? == 1));then
echo -n "standard server ${port:-9000} to start......"
path=$(dirname $0)
if [[ $path != '.' ]];then
cd $path
fi
# nohup /home/seemmo/share/python/centos/python3.6.6/bin/python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
nohup python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
while true
do
check
if (($? == 1));then
echo -n '...'
sleep 1
else
echo -e '\033[32mstarted\033[1m\033[0m'
break
fi
done
else
echo "standard server ${port:-9000} has been running!!!"
fi
} fstop()
{
check
if (($? == 1));then
echo "standard server ${port:-9000} has been stopped!!!"
else
echo -n "standard server ${port:-9000} force to stop....."
ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs kill -9
while true
do
check
if (($? == 1));then
echo -e '\033[32mstopped\033[1m\033[0m'
break
else
echo -n '...'
sleep 1
fi
done
fi
} stop()
{
check
if (($? == 1));then
echo "standard server ${port:-9000} has been stopped!!!"
else
echo -n "standard server ${port:-9000} to stop....."
spid=1
tp_list=($(ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs))
for tpid in ${tp_list[@]}
do
if ((spid == 1));then
spid=$tpid
elif ((tpid < spid));then
spid=$tpid
fi
done
kill -15 $spid
retry_time=3
while true
do
if ((retry_time == 0));then
echo
fstop
break
fi
check
if (($? == 1));then
echo -e '\033[32mstopped\033[1m\033[0m'
break
else
((retry_time=retry_time-1))
echo -n '.'
sleep 1
fi
done
fi
} status()
{
check
if (($? == 1));then
echo -e "standard server ${port:-9000} now is \033[32mstopped\033[1m\033[0m"
else
echo -e "standard server ${port:-9000} now is \033[32mrunning\033[1m\033[0m"
fi
} running()
{
port_list=$(ps aux | grep "manage.py" | grep -v "grep" |awk '{print $17}' |xargs)
for port in ${port_list}
do
echo -e "standard server ${port} now is \033[32mrunning\033[1m\033[0m"
done
} restart() {
check
if (($? == 1));then
start
else
stop
while true
do
check
if (($? == 1));then
start
break
else
sleep 1
fi
done
fi
} if (($# == 1)) || (($# == 3));then
if (($# == 3));then
host=$2
port=$3
fi case $1 in
start|stop|status|restart|fstop|running)
$1
;;
*)
if (($# == 1));then
echo "Usage: bash $0 {start|stop|status|restart|fstop|running}"
exit 2
else
echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
exit 2
fi
esac else
echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
exit 2
fi

ending ~

上一篇:GreenPlum高效去除表重复数据


下一篇:RESTful API 编写指南