Linux 系统下 nginx 服务脚本(重新整理)
来源:靑龍一笑的博客 作者:靑龍一笑 发布时间:2019-01-23 14:36:53 点击量:1139 评论:0
最近遇到过几次因进程名或端口冲突,而导致 nginx 服务脚本失效的问题。为此,更新了一下 nginx 的系统服务脚本。
内容如下:
#!/bin/sh
# chkconfig: 35 78 32
# Description: nginx 系统服务脚本
# 作者:靑龍一笑(C.S.Ricen)
# 使用说明:将此脚本扔到 /etc/init.d 目录下。
# 针对 Debian 系列版本,运行命令:update-rc.d -f nginx defaults
# 针对 RedHat 系列版本,运行命令:chkconfig --add nginx
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/opt/nginx/sbin/$NAME
CONFIGFILE=/opt/nginx/conf/$NAME.conf
PIDFILE=/opt/nginx/logs/$NAME.pid
case "$1" in
start)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$NAME (进程 $NGINX_PID) 正在运行 ..."
exit 1
fi
done
else
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo "$NAME 启动失败!建议先停止再启动!如果还是失败,请与技术人员联系!"
exit 1
else
echo "$NAME 启动成功!"
exit 0
fi
fi
;;
stop)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo "$NAME 停止失败!"
exit 1
else
echo "$NAME 停止成功!"
exit 0
fi
else
echo "$NAME (进程 $NGINX_PID) 不存在。"
exit 1
fi
done
else
echo "$NAME 已经停止。如果进程仍然存在,请与技术人员联系!"
exit 0
fi
;;
status)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$NAME (进程 $NGINX_PID) 正在运行 ..."
fi
done
else
echo "$NAME 已经停止。如果进程仍然存在,请与技术人员联系!"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
$NGINX_BIN -s reload
echo "$NAME 重载完成!"
exit 0
fi
done
else
echo "$NAME 没有运行,不能重载!"
exit 1
fi
;;
configtest)
echo "测试 $NAME 配置文件 ... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 1
;;
esac
# chkconfig: 35 78 32
# Description: nginx 系统服务脚本
# 作者:靑龍一笑(C.S.Ricen)
# 使用说明:将此脚本扔到 /etc/init.d 目录下。
# 针对 Debian 系列版本,运行命令:update-rc.d -f nginx defaults
# 针对 RedHat 系列版本,运行命令:chkconfig --add nginx
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/opt/nginx/sbin/$NAME
CONFIGFILE=/opt/nginx/conf/$NAME.conf
PIDFILE=/opt/nginx/logs/$NAME.pid
case "$1" in
start)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$NAME (进程 $NGINX_PID) 正在运行 ..."
exit 1
fi
done
else
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo "$NAME 启动失败!建议先停止再启动!如果还是失败,请与技术人员联系!"
exit 1
else
echo "$NAME 启动成功!"
exit 0
fi
fi
;;
stop)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo "$NAME 停止失败!"
exit 1
else
echo "$NAME 停止成功!"
exit 0
fi
else
echo "$NAME (进程 $NGINX_PID) 不存在。"
exit 1
fi
done
else
echo "$NAME 已经停止。如果进程仍然存在,请与技术人员联系!"
exit 0
fi
;;
status)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$NAME (进程 $NGINX_PID) 正在运行 ..."
fi
done
else
echo "$NAME 已经停止。如果进程仍然存在,请与技术人员联系!"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
if [ -f "$PIDFILE" ]; then
cat $PIDFILE | while read line
do
NGINX_PID=$line
ps -ax | awk '{print $1}' | grep -e $NGINX_PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
$NGINX_BIN -s reload
echo "$NAME 重载完成!"
exit 0
fi
done
else
echo "$NAME 没有运行,不能重载!"
exit 1
fi
;;
configtest)
echo "测试 $NAME 配置文件 ... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 1
;;
esac
版权所有 © 2005-2023 靑龍一笑的博客 Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.