centos6.4下NGINX启动脚本

#!/bin/bash
# nginx     This shell script takes care of starting and stopping
#           nginx
#
# chkconfig: - 13 68
# description: nginx is a web server
### BEGIN INIT INFO
# Provides: $named
# Short-Description: start|stop|status|restart|configtest 
### END INIT INFO
#variables
NGINX_BIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
NETSTAT="/bin/netstat"
alter=$1
prog=nginx
#load system function
. /etc/rc.d/init.d/functions
#function:echo ok or error
function if_no {
if [ $2 == 0 ]; then
echo -n $"$1 ${prog}:" && success && echo
else
echo -n $"$1 ${prog}:" && failure && echo
fi
}
#start nginx
function start {
if [ -s ${NGINX_PID} ]; then
echo "nginx already running" 
else
if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
${NGINX_BIN} -c ${NGINX_CONF} 
if_no start $?
        else
${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1 > ${NGINX_PID}
if_no start $?
fi
fi
}
#stp nginx
function stop {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -QUIT
if_no stop $?
else
        if [ `${NETSTAT} -tnpl | grep nginx | wc -l` -eq 0 ]; then
rm -f ${NGINX_PID} 2>/dev/null
if_no stop 0
else
rm -f ${NGINX_PID} 2>/dev/null
kill `${NETSTAT} -tnpl | grep nginx | awk '{ print $7}' | cut -d '/' -f 1`
if_no stop $?
fi
fi
}
function restart {
if [ -s ${NGINX_PID} ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_no restart $?
else
stop
sleep 1
start
fi
}
function status {
${NETSTAT} -tnpl | grep nginx | grep LISTEN
[ $? == 0 ] && echo "nginx is running" || echo "nginx is not running"
}
function configtest {
${NGINX_BIN} -t
}
case $alter in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
configtest)
configtest
;;
*)
echo "use:${NGINX} {start|stop|restart|status|configtest}"
;;
esac


转载于:https://my.oschina.net/zijian1315/blog/208654

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值