]>
Commit | Line | Data |
---|---|---|
9d23b01f | 1 | #!/bin/sh |
2 | # | |
a8a40c5a | 3 | # nginx Nginx Web Server (@type@ version) |
9d23b01f | 4 | # |
5 | # chkconfig: 345 85 15 | |
e081a5bc ER |
6 | # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ |
7 | # proxy and IMAP/POP3 proxy server | |
61cffbcc ER |
8 | # processname: nginx |
9 | # pidfile: /var/run/nginx.pid | |
10 | # config: /etc/nginx/nginx.conf | |
9d23b01f | 11 | |
9d23b01f | 12 | # Source function library |
13 | . /etc/rc.d/init.d/functions | |
14 | ||
e081a5bc | 15 | # Source networking configuration. |
9d23b01f | 16 | . /etc/sysconfig/network |
17 | ||
61cffbcc ER |
18 | nginx="/usr/sbin/nginx" |
19 | svname="nginx" | |
e081a5bc ER |
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 | ||
9d23b01f | 28 | # Get service config |
e081a5bc | 29 | [ -f $sysconfig ] && . $sysconfig |
9d23b01f | 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 | |
e081a5bc | 34 | msg_network_down "$svname" |
9d23b01f | 35 | exit 1 |
36 | fi | |
37 | else | |
38 | exit 0 | |
39 | fi | |
40 | ||
ab8302f0 ER |
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 | ||
9d23b01f | 77 | start() { |
78 | # Check if the service is already running? | |
e081a5bc | 79 | if [ ! -f $lockfile ]; then |
ab8302f0 | 80 | checkconfig |
e081a5bc ER |
81 | msg_starting "$svname" |
82 | daemon $nginx -c $NGINX_CONF_FILE | |
9d23b01f | 83 | RETVAL=$? |
e081a5bc | 84 | [ $RETVAL -eq 0 ] && touch $lockfile |
9d23b01f | 85 | else |
e081a5bc | 86 | msg_already_running "$svname" |
9d23b01f | 87 | fi |
88 | } | |
89 | ||
90 | stop() { | |
23494879 AM |
91 | local oldbin_pidfile="${pidfile}.oldbin" |
92 | ||
9d23b01f | 93 | # Stop daemons. |
e081a5bc | 94 | if [ -f $lockfile ]; then |
23494879 AM |
95 | if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then |
96 | msg_stopping "$svname (old process)" | |
97 | killproc -p $oldbin_pidfile $prog -TERM | |
98 | fi | |
e081a5bc | 99 | msg_stopping "$svname" |
65840609 ER |
100 | killproc -p $pidfile $prog |
101 | RETVAL=$? | |
ff4955e8 | 102 | rm -f $lockfile $pidfile >/dev/null 2>&1 |
9d23b01f | 103 | else |
e081a5bc | 104 | msg_not_running "$svname" |
9d23b01f | 105 | fi |
106 | } | |
107 | ||
149be10d ER |
108 | reload() { |
109 | if [ -f $lockfile ]; then | |
ab8302f0 | 110 | checkconfig |
149be10d | 111 | msg_reloading "$svname" |
65840609 | 112 | killproc -p $pidfile $prog -HUP |
149be10d ER |
113 | RETVAL=$? |
114 | else | |
115 | msg_not_running "$svname" | |
116 | RETVAL=7 | |
117 | fi | |
118 | } | |
119 | ||
120 | condrestart() { | |
121 | if [ ! -f $lockfile ]; then | |
122 | msg_not_running "$svname" | |
123 | RETVAL=$1 | |
124 | return | |
125 | fi | |
126 | ||
ab8302f0 | 127 | checkconfig |
149be10d ER |
128 | stop |
129 | start | |
130 | } | |
131 | ||
27d3b195 ER |
132 | # Upgrade the binary with no downtime. |
133 | # http://nginx.org/en/docs/control.html#upgrade | |
134 | # TODO: handle revert back on failed upgrade | |
135 | upgrade() { | |
10f92d73 | 136 | local oldbin_pidfile="${pidfile}.oldbin" retry |
27d3b195 ER |
137 | |
138 | checkconfig | |
139 | show "Upgrading $svname" | |
140 | killproc -p $pidfile $prog -USR2 | |
141 | RETVAL=$? | |
10f92d73 | 142 | |
b2f0901d AM |
143 | # wait for 3m |
144 | retry=720 | |
10f92d73 AM |
145 | while [ $retry -gt 0 ]; do |
146 | if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then | |
b2f0901d AM |
147 | show "Upgrade: stopping old workers" |
148 | killproc -p $oldbin_pidfile $prog -WINCH | |
149 | show "Upgrade: stopping old master process" | |
10f92d73 AM |
150 | killproc -p $oldbin_pidfile $prog -QUIT |
151 | return 0 | |
152 | else | |
153 | usleep 250000 | |
154 | retry=$(($retry -1)) | |
155 | fi | |
156 | done | |
157 | ||
158 | show "Upgrade: stopping old process"; fail | |
159 | nls 'old process pid file was not found' | |
160 | return 1 | |
27d3b195 ER |
161 | } |
162 | ||
f80bc012 ER |
163 | # Tell nginx to reopen logs |
164 | # http://nginx.org/en/docs/control.html#logs | |
165 | reopen_logs() { | |
fa88cb53 AM |
166 | local oldbin_pidfile="${pidfile}.oldbin" |
167 | ||
168 | if [ -f $oldbin_pidfile ]; then | |
169 | show "Reopening $svname (oldbin) logs" | |
170 | killproc -p $oldbin_pidfile $prog -USR1 | |
171 | fi | |
172 | ||
f80bc012 ER |
173 | show "Reopening $svname logs" |
174 | killproc -p $pidfile $prog -USR1 | |
175 | } | |
176 | ||
9d23b01f | 177 | RETVAL=0 |
178 | # See how we were called. | |
179 | case "$1" in | |
180 | start) | |
149be10d | 181 | start |
9d23b01f | 182 | ;; |
183 | stop) | |
149be10d | 184 | stop |
9d23b01f | 185 | ;; |
186 | restart) | |
ab8302f0 | 187 | checkconfig |
9d23b01f | 188 | stop |
189 | start | |
190 | ;; | |
149be10d ER |
191 | try-restart) |
192 | condrestart 0 | |
193 | ;; | |
27d3b195 | 194 | reload|graceful) |
149be10d ER |
195 | reload |
196 | ;; | |
f50d7667 | 197 | force-reload|upgrade) |
27d3b195 ER |
198 | upgrade |
199 | ;; | |
f80bc012 ER |
200 | reopen-logs) |
201 | reopen_logs | |
202 | ;; | |
ab8302f0 ER |
203 | checkconfig|configtest) |
204 | checkconfig 1 | |
205 | ;; | |
149be10d | 206 | status) |
65840609 | 207 | status --pidfile $pidfile $prog |
149be10d | 208 | RETVAL=$? |
9d23b01f | 209 | ;; |
210 | *) | |
f80bc012 | 211 | msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|graceful|reopen-logs|checkconfig|status}" |
9d23b01f | 212 | exit 3 |
213 | ;; | |
214 | esac | |
215 | ||
216 | exit $RETVAL |