WangYan Blog

WangYan's Blog 发现、实践、分享!

2

nginx开机自启动init脚本

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

一、使用方法

1、安装

1
2
3
4
wget http://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、卸载

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

3、使用实例

1
2
3
4
service nginx stop
service nginx start
service nginx restart
service nginx reload

二、脚本内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /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
原文地址 : https://wangyan.org/blog/nginx-init-script.html
本站遵循 : 知识共享署名-非商业性使用-相同方式共享 3.0 版权协议
版权声明 : 原创文章转载时,请务必以超链接形式标明 文章原始出处
作者:WangYan | 分类:学习笔记 | 标签: script, nginx, shell
上一篇:VPS自动监控Shell脚本 | 下一篇:计算机存储单位辨析

已有 2 条评论 »

发表新评论 »
  1. Bisn
    Bisn 2011/09/30

    你的文章编排和代码好漂亮,请问你用的什么编辑器和代码高亮工具?

  2. centos
    centos 2011/10/14

    嗯,加了这些脚本管理起来方便很多

发表新评论 »