]> git.pld-linux.org Git - packages/nginx.git/blob - nginx.init
- release 2 (by relup.sh)
[packages/nginx.git] / nginx.init
1 #!/bin/sh
2 #
3 # nginx Nginx Web Server (@type@ version)
4 #
5 # chkconfig:    345 85 15
6 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
7 #               proxy and IMAP/POP3 proxy server
8 # processname:  nginx
9 # pidfile:      /var/run/nginx.pid
10 # config:       /etc/nginx/nginx.conf
11
12 # Source function library
13 . /etc/rc.d/init.d/functions
14
15 # Source networking configuration.
16 . /etc/sysconfig/network
17
18 nginx="/usr/sbin/nginx"
19 svname="nginx"
20 prog=${nginx##*/}
21
22 sysconfig="/etc/sysconfig/$prog"
23 lockfile="/var/lock/subsys/$prog"
24 pidfile="/var/run/$prog.pid"
25
26 NGINX_CONF_FILE="/etc/nginx/$prog.conf"
27
28 # Get service config
29 [ -f $sysconfig ] && . $sysconfig
30
31 # Check that networking is up.
32 if is_yes "${NETWORKING}"; then
33         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
34                 msg_network_down "$svname"
35                 exit 1
36         fi
37 else
38         exit 0
39 fi
40
41 # configtest itself
42 # must return non-zero if check failed
43 # output is discarded if checkconfig is ran without details
44 configtest() {
45         $nginx -t -c $NGINX_CONF_FILE
46 }
47
48 # wrapper for configtest
49 checkconfig() {
50         local details=${1:-0}
51
52         if [ $details = 1 ]; then
53                 # run config test and display report (status action)
54                 show "Checking %s configuration" "$svname"; busy
55                 local out
56                 out=$(configtest 2>&1)
57                 RETVAL=$?
58                 if [ $RETVAL = 0 ]; then
59                         ok
60                 else
61                         fail
62                 fi
63                 [ "$out" ] && echo >&2 "$out"
64         else
65                 # run config test and abort with nice message if failed
66                 # (for actions checking status before action).
67                 configtest >/dev/null 2>&1
68                 RETVAL=$?
69                 if [ $RETVAL != 0 ]; then
70                         show "Checking %s configuration" "$svname"; fail
71                         nls 'Configuration test failed. See details with %s "checkconfig"' $0
72                         exit $RETVAL
73                 fi
74         fi
75 }
76
77 start() {
78         # Check if the service is already running?
79         if [ ! -f $lockfile ]; then
80                 checkconfig
81                 msg_starting "$svname"
82                 daemon $nginx -c $NGINX_CONF_FILE
83                 RETVAL=$?
84                 [ $RETVAL -eq 0 ] && touch $lockfile
85         else
86                 msg_already_running "$svname"
87         fi
88 }
89
90 stop() {
91         # Stop daemons.
92         if [ -f $lockfile ]; then
93                 msg_stopping "$svname"
94                 killproc -p $pidfile $prog
95                 RETVAL=$?
96                 rm -f $lockfile $pidfile >/dev/null 2>&1
97         else
98                 msg_not_running "$svname"
99         fi
100 }
101
102 reload() {
103         if [ -f $lockfile ]; then
104                 checkconfig
105                 msg_reloading "$svname"
106                 killproc -p $pidfile $prog -HUP
107                 RETVAL=$?
108         else
109                 msg_not_running "$svname"
110                 RETVAL=7
111         fi
112 }
113
114 condrestart() {
115         if [ ! -f $lockfile ]; then
116                 msg_not_running "$svname"
117                 RETVAL=$1
118                 return
119         fi
120
121         checkconfig
122         stop
123         start
124 }
125
126 # Upgrade the binary with no downtime.
127 # http://nginx.org/en/docs/control.html#upgrade
128 # TODO: handle revert back on failed upgrade
129 upgrade() {
130         local oldbin_pidfile="${pidfile}.oldbin"
131
132         checkconfig
133         show "Upgrading $svname"
134         killproc -p $pidfile $prog -USR2
135         RETVAL=$?
136         sleep 1
137         if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then
138                 show "Upgrade: stopping old process"
139                 killproc -p $oldbin_pidfile $prog -QUIT
140                 return 0
141         else
142                 return 1
143         fi
144 }
145
146 # Tell nginx to reopen logs
147 # http://nginx.org/en/docs/control.html#logs
148 reopen_logs() {
149         show "Reopening $svname logs"
150         killproc -p $pidfile $prog -USR1
151 }
152
153 RETVAL=0
154 # See how we were called.
155 case "$1" in
156   start)
157         start
158         ;;
159   stop)
160         stop
161         ;;
162   restart)
163         checkconfig
164         stop
165         start
166         ;;
167   try-restart)
168         condrestart 0
169         ;;
170   reload|graceful)
171         reload
172         ;;
173   force-reload|upgrade)
174         upgrade
175         ;;
176   reopen-logs)
177         reopen_logs
178         ;;
179   checkconfig|configtest)
180         checkconfig 1
181         ;;
182   status)
183         status --pidfile $pidfile $prog
184         RETVAL=$?
185         ;;
186   *)
187         msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|graceful|reopen-logs|checkconfig|status}"
188         exit 3
189         ;;
190 esac
191
192 exit $RETVAL
This page took 0.115393 seconds and 3 git commands to generate.