欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-06-08 22:17:28
第一步 先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/ng...
第一步
先运行命令关闭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