导航
导航
文章目录
  1. 一、使用方法
  2. 二、脚本内容

nginx开机自启动init脚本

适用于源码编译安装nginx的用户,支持RedHat、CentOS、Debian、Ubuntu等发行版,支持start|stop|restart|reload 四个基本操作,非常简单实用。

一、使用方法

1、安装

wget http://blog.wangyan.org/download/conf/init.d.nginx -P /etc/init.d/nginx
chmod 755  /etc/init.d/nginx
chkconfig --add nginx (RedHat)
update-rc.d -f nginx defaults (debian)

2、卸载

chkconfig --del nginx (RedHat)
update-rc.d -f nginx remove (debian)

3、使用实例

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

二、脚本内容

#! /bin/bash
#
# nginx      Start up the nginx server daemon
#
# chkconfig: 2345 55 25
# Description: starts and stops the nginx web server
#
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       starts and stops the nginx web server
### END INIT INFO

# Author: WangYan <webmaster@wangyan.org>
# Version:1.0 (2011.19.15)

# To install:
#   copy this file to /etc/init.d/nginx
#   shell> chkconfig --add nginx (RedHat)
#   shell> update-rc.d -f nginx defaults (debian)

# To uninstall:
#   shell> chkconfig --del nginx (RedHat)
#   shell> update-rc.d -f nginx remove

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/var/run/$NAME.pid
ULIMIT=10240

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
    echo "Starting $NAME ..."
    ulimit -SHn $ULIMIT
    $DAEMON -c $CONFIGFILE
}

do_stop() {
    echo "Shutting down $NAME ..."
    kill `cat $PIDFILE`
}

do_reload() {
    echo "Reloading $NAME ..."
    kill -HUP `cat $PIDFILE`
}

case "$1" in
    start)
        [ ! -f "$PIDFILE" ] && do_start || echo "nginx already running"
    echo -e ".\ndone"
        ;;
    stop)
        [ -f "$PIDFILE" ] && do_stop || echo "nginx not running"
    echo -e ".\ndone"
        ;;
    restart)
        [ -f "$PIDFILE" ] && do_stop || echo "nginx not running"
        do_start
    echo -e ".\ndone"
        ;;
    reload)
        [ -f "$PIDFILE" ] && do_reload || echo "nginx not running"
    echo -e ".\ndone"
        ;;
    *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload}" >&2
    exit 1
    ;;
esac

exit 0
支持一下
扫一扫,支持一下