脚本可以启动服务、关闭服务、重启服务、查看版本、测试配置文件
代码:
#!/bin/bash
scriptname=$(echo $0 | awk -F "/" '{print $2}')
function chkservice()
{
PORT_C=$(ss -anu |grep -c $2)
PS_C=$(ps -ef |grep $1 |grep -vc grep)
if [ $PORT_C -eq 0 -o $PS_C -eq 0 ]; then
r=1
else
r=0
fi
return $r
}
nginxcmd=/usr/local/nginx/sbin/nginx
nginxconfopt="/usr/local/nginx/conf/nginx.conf"
nginxtestconf=" -t"
nginxstopopt=" -s stop"
nginxrestartopt=" -s reload"
nginxversion=" -v"
if [ -z $1 ]; then
echo "please input option:start|stop|restart|test|version";
exit 1
fi
if [ -f $nginxcmd ]; then
echo "command ready..."
else
echo "nginx command is not exit!"
exit 1
fi
if [ -f $nginxconfopt ]; then
echo "configure file reday..."
else
echo "not found $nginxconf"
exit 1
fi
chkservice nginx 80
r=$?
case $1 in
start|begin)
if [ $r -eq 1 ]; then
echo $nginxcmd "-c" $nginxconfopt|bash
else
echo "nginx servie already run!"
fi
;;
stop|end)
if [ $r -eq 0 ]; then
echo $nginxcmd $nginxstopopt|bash
else
echo "nginx service not found!"
fi
;;
restart|reload)
echo $nginxcmd $nginxrestartopt|bash
;;
test)
echo $nginxcmd $nginxtestconf|bash
;;
version)
echo $nginxcmd $nginxversion|bash
;;
*)
echo "please input option:start|stop|restart|testi|version";
;;
esac