-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnginx.init
More file actions
executable file
·216 lines (191 loc) · 4.13 KB
/
nginx.init
File metadata and controls
executable file
·216 lines (191 loc) · 4.13 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
#
# nginx Nginx Web Server (@type@ version)
#
# chkconfig: 345 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /etc/nginx/nginx.conf
# Source function library
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
nginx="/usr/sbin/nginx"
svname="nginx"
prog=${nginx##*/}
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/$prog"
pidfile="/var/run/$prog.pid"
NGINX_CONF_FILE="/etc/nginx/$prog.conf"
# Get service config
[ -f $sysconfig ] && . $sysconfig
# Check that networking is up.
if is_yes "${NETWORKING}"; then
if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
msg_network_down "$svname"
exit 1
fi
else
exit 0
fi
# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
# wrapper for configtest
checkconfig() {
local details=${1:-0}
if [ $details = 1 ]; then
# run config test and display report (status action)
show "Checking %s configuration" "$svname"; busy
local out
out=$(configtest 2>&1)
RETVAL=$?
if [ $RETVAL = 0 ]; then
ok
else
fail
fi
[ "$out" ] && echo >&2 "$out"
else
# run config test and abort with nice message if failed
# (for actions checking status before action).
configtest >/dev/null 2>&1
RETVAL=$?
if [ $RETVAL != 0 ]; then
show "Checking %s configuration" "$svname"; fail
nls 'Configuration test failed. See details with %s "checkconfig"' $0
exit $RETVAL
fi
fi
}
start() {
# Check if the service is already running?
if [ ! -f $lockfile ]; then
checkconfig
msg_starting "$svname"
daemon $nginx -c $NGINX_CONF_FILE
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $lockfile
else
msg_already_running "$svname"
fi
}
stop() {
local oldbin_pidfile="${pidfile}.oldbin"
# Stop daemons.
if [ -f $lockfile ]; then
if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then
msg_stopping "$svname (old process)"
killproc -p $oldbin_pidfile $prog -TERM
fi
msg_stopping "$svname"
killproc -p $pidfile $prog
RETVAL=$?
rm -f $lockfile $pidfile >/dev/null 2>&1
else
msg_not_running "$svname"
fi
}
reload() {
if [ -f $lockfile ]; then
checkconfig
msg_reloading "$svname"
killproc -p $pidfile $prog -HUP
RETVAL=$?
else
msg_not_running "$svname"
RETVAL=7
fi
}
condrestart() {
if [ ! -f $lockfile ]; then
msg_not_running "$svname"
RETVAL=$1
return
fi
checkconfig
stop
start
}
# Upgrade the binary with no downtime.
# http://nginx.org/en/docs/control.html#upgrade
# TODO: handle revert back on failed upgrade
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin" retry
checkconfig
show "Upgrading $svname"
killproc -p $pidfile $prog -USR2
RETVAL=$?
# wait for 3m
retry=720
while [ $retry -gt 0 ]; do
if [ -f $oldbin_pidfile ] && [ -f $pidfile ]; then
show "Upgrade: stopping old workers"
killproc -p $oldbin_pidfile $prog -WINCH
show "Upgrade: stopping old master process"
killproc -p $oldbin_pidfile $prog -QUIT
return 0
else
usleep 250000
retry=$(($retry -1))
fi
done
show "Upgrade: stopping old process"; fail
nls 'old process pid file was not found'
return 1
}
# Tell nginx to reopen logs
# http://nginx.org/en/docs/control.html#logs
reopen_logs() {
local oldbin_pidfile="${pidfile}.oldbin"
if [ -f $oldbin_pidfile ]; then
show "Reopening $svname (oldbin) logs"
killproc -p $oldbin_pidfile $prog -USR1
fi
show "Reopening $svname logs"
killproc -p $pidfile $prog -USR1
}
RETVAL=0
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
checkconfig
stop
start
;;
try-restart)
condrestart 0
;;
reload|graceful)
reload
;;
force-reload|upgrade)
upgrade
;;
reopen-logs)
reopen_logs
;;
checkconfig|configtest)
checkconfig 1
;;
status)
status --pidfile $pidfile $prog
RETVAL=$?
;;
*)
msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|graceful|reopen-logs|checkconfig|status}"
exit 3
;;
esac
exit $RETVAL