這是控制nginx服務的腳本文件,包括控制nginx的啓動、重啓、中止、平滑重啓、對配置文件的額檢查。nginx
[root@localhost ~]# cat nginx.sh bash
#!/bin/env bashide
# description:nginx server ###必須加描述this
# nginx - this script is used to control nginx servicespa
# processname nginxrest
# chkconfig: - 85 15server
# edit by sunip
# date 2014-07-03it
# email address 1305627792@qq.comio
nginx="/usr/local/nginx/sbin/nginx"
prog="nginx"
conf_file="/usr/local/nginx/conf/nginx.conf"
start() {
if [ `pgrep $prog | wc -l` -eq 0 ];then
if [ -x $nginx ] && [ -f $conf_file ];then
$nginx -c $conf_file
ret=$?
if [ $ret -eq 0 ];then
echo "$prog start successed"
else
echo "$prog start failed"
fi
else
echo "$prog config file not exist"
fi
else
echo "$prog is already started ... "
fi
}
stop() {
if [ `pgrep $prog | wc -l` -ne 0 ];then
killall -9 $prog
ret=$?
if [ $ret -eq 0 ];then
echo "$prog stop successed"
else
echo "$prog stop failed"
fi
else
echo "$prog is already stopped ..."
fi
}
restart() {
stop
sleep 2
start
}
reload() {
if [ `pgrep $prog | wc -l` -ne 0 ];then
pid=`ps -ef | grep $prog | grep master | awk '{print $2}'`
if [ -x $nginx ] && [ -f $conf_file ];then
kill -HUP $pid
ret=$?
if [ $ret -eq 0 ];then
echo "$prog reload successed"
else
echo "$prog reload failed"
fi
else
echo "$prog config file is not exist"
fi
else
echo "$prog is stopped, please start $prog first ..."
fi
}
check() {
if [ -x $nginx ] && [ -f $conf_file ];then
$nginx -t -c $conf_file
ret=$?
if [ $ret -eq 0 ];then
echo "$prog check successed"
else
echo "$prog check failed"
fi
else
echo "$prog program or config file not exit!"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
check)
check
;;
*)
echo "Usage: $0 {start|stop|restart|reload|check}"
esac
運行bash nginx.sh start等操做便可。