Nginx 启动脚本/重启脚本代码_nginx

第一步
先运行命令关闭nginx

sudo kill `cat /usr/local/nginx/logs/nginx.pid`
第二步

vi /etc/init.d/nginx
输入以下内容

复制代码 代码如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1

stop)
rh_status_q || exit 0
$1

restart|configtest)
$1

reload)
rh_status_q || exit 7
$1

force-reload)
force_reload

status)
rh_status

condrestart|try-restart)
rh_status_q || exit 0

*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

保存退出

第三步

chmod +x /etc/init.d/nginx
第四步

/sbin/chkconfig nginx on
检查一下

sudo /sbin/chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
完成!

之后,就可以使用以下命令了

复制代码 代码如下:

service nginx start
service nginx stop
service nginx restart
service nginx reload

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload

下面是其它作者发布的文章

复制代码 代码如下:

#vi /etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# Provides: Nginx-php-fpm(fastcgi)
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop nginx-fcgi in external FASTCGI mode
# Description: Start and stop nginx-fcgi in external FASTCGI mode
# http://www.linxutone.org msn:cnseek@msn.com
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

d_start() {
/usr/local/php-fcgi/sbin/php-fpm start > /dev/null 2>&1
$DAEMON -c $CONFIGFILE || echo -n " already running"
}

d_stop() {
/usr/local/php-fcgi/sbin/php-fpm stop > /dev/null 2>&1
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

d_reload() {
/usr/local/php-fcgi/sbin/php-fpm reload > /dev/null 2>&1
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."

stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."

reload)
echo -n "Reloading $DESC configuration ..."
d_reload
echo "reloaded."

restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."

*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3

esac

exit 0

#chmod u+x /etc/init.d/nginx

使用方法:

复制代码 代码如下:

#/etc/init.d/nginx start
#/etc/init.d/nginx stop
#/etc/init.d/nginx restart

注意修改安装路径了

复制代码 代码如下:

#!/bin/bash
#
# Init file for nginx server daemon
#
# chkconfig: 234 99 99
# description: nginx server daemon
#
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
RETVAL=0
prog="nginx"
PAT=/usr/local/nginx
NGINXD=/usr/local/nginx/sbin/nginx
PID_FILE=/usr/local/nginx/nginx.pid
start()
{
echo -n $"Starting $prog: "
$NGINXD 2>/dev/null $OPTIONS && success || failure
RETVAL=$?
[ "$RETVAL" = 0 ] && touch /var/lock/subsys/nginx
echo
}
stop()
{
echo -n $"Shutting down $prog: "
killproc nginx
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/nginx
return $RETVAL
}
reload()
{
echo -n $"Reloading nginx: "
killproc nginx -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start

stop)
stop

restart)
stop
start

reload)
reload

status)
status -p $PID_FILE nginx
RETVAL=$?

*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
RETVAL=1
esac
exit $RETVAL

时间: 2024-07-29 10:19:28

Nginx 启动脚本/重启脚本代码_nginx的相关文章

centos下nginx启动、重启、关闭操作

Nginx的启动 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 其中 -c 参数指定配置文件路径. Nginx的停止 Nginx支持以下几种信号控制: TERM, INT 快速关闭 QUIT 从容关闭 HUP 平滑重启 USR1 重新打开日志文件,在切割文件时用处大 USR2 平滑升级 WINCH 从容关闭工作进程 我们可以通过信号停止Nginx主进程,首先,我们需要通过 ps -ef | grep 命令获得mas

Nginx启动/停止/状态检查/重新启动/平滑重启脚本

默认使用源代码安装Nginx时,Ngninx需要另外配置启动/停止/状态检查/ 重新启动/平滑重启/测试配置文件等功能的脚本,风信网收集并整理了一段Nginx的功能脚本,供大家参考使用. 应用新脚本之前,需要先运行命令关闭nginx [root@localhost ~]# sudo kill `cat /usr/local/nginx/logs/nginx.pid` #请修改为你的nginx.pid文件路径 建立一个nginx启动脚本 [root@localhost ~]# vi /etc/in

Nginx和PHP-FPM的启动、重启、停止脚本分享_linux shell

服务器上的Nginx和PHP都是源码编译安装的,不像ubuntu一样有自带service启动脚本,所以不支持类似以前的nginx (start|restart|stop|reload)了.自己动手丰衣足食.以下脚本应该在RHEL, Fedora, CentOS下都适用. 一.Nginx启动脚本/etc/init.d/nginx 复制代码 代码如下: #!/bin/bash # # Startup script for Nginx - this script starts and stops th

Nginx和PHP-FPM的启动/重启脚本分享

一.Nginx启动脚本/etc/init.d/nginx .  代码如下 复制代码 #!/bin/bash # # Startup script for Nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server #

nginx自动启动脚本几段代码

ginx源码安装完后不能使用/etc/init.d/XX方式重启等,所以写了个启动脚本. 原理还是利用了nginx自己的启动脚本,只是进行了简单的整合. 把以下脚本保存为nginx文件放入/etc/init.d/nginx 然后可以通过 /etc/init.d/nginx start 命令启动nginx /etc/init.d/nginx stop 命令停止nginx /etc/init.d/nginx restart 命令重启nginx 开机自动启动nginx, 如果需要开机启动服务,保存好

centos系统中nginx启动脚本和chkconfig管理

在安装完nginx后,重新启动需要"kill -HUP nginx进程编号"来进行重新加载,显然十分不方便.如果能像apache一样,直接通过脚本进行管理就方便多了. nginx官方早就想好了,也提供了这个脚本,地址:http://wiki.nginx.org/RedHatNginxInitScript.这里将管理脚本收录在这里:  代码如下 复制代码 #!/bin/sh # # nginx - this script starts and stops the nginx daemon

linux下oracle重启、启动、停止脚本

网站的服务中断了,重启下发现是oralce服务不存在,又不想重启机器,就重新启动下oralce,再重启服务,搞定. 操作的为oracle9i:(其他应该也可以用吧记录如下)声明:坚挺器(应该理解的哦,信息发不出去,你懂的,就用这个了) 手动操作  代码如下 复制代码 (1) 以oracle身份登录数据库,命令:su – oracle (2) 进入Sqlplus控制台,命令:sqlplus /nolog (3) 以系统管理员登录,命令:connect / as sysdba (4) 启动数据库,命

Nginx 启动、停止、重启、升级操作命令收集_nginx

那下面主要总结一下Nginx的基本操作. 启动操作 命令: nginx -c /usr/nginx/conf/nginx.conf -c参数指定了要加载的nginx配置文件路径. 停止操作 停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文章)来进行的 步骤1:查询nginx主进程号 ps -ef | grep nginx 在进程列表里面找master进程,它的编号就是主进程号了. 步骤2:发送信号 从容停止Nginx: kill -QUIT 主进程号 快速停止Nginx: k

php管理nginx虚拟主机shell脚本实例_php技巧

本文实例讲述了php管理nginx虚拟主机shell脚本,分享给大家供大家参考.具体分析如下: 使用php作为shell脚本是一件很方便的事情.理所当然,我们可以使用php脚本来管理 nginx虚拟主机,下面是笔者的 脚本 文件供各位参考: 复制代码 代码如下: #!/usr/bin/php -q <?php   start: fwrite(STDOUT,"===========Vhost Script===========\n"); fwrite(STDOUT,"=