· 9 years ago · Oct 08, 2016, 08:34 PM
1#! /bin/sh
2### BEGIN INIT INFO
3# Provides: nginx
4# Required-Start: $remote_fs $syslog
5# Required-Stop: $remote_fs $syslog
6# Default-Start: 2 3 4 5
7# Default-Stop: 0 1 6
8# Short-Description: nginx init.d dash script for Ubuntu or other *nix.
9# Description: nginx init.d dash script for Ubuntu or other *nix.
10### END INIT INFO
11#------------------------------------------------------------------------------
12# nginx - this Debian Almquist shell (dash) script, starts and stops the nginx
13# daemon for Ubuntu and other *nix releases.
14#
15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
16# proxy and IMAP/POP3 proxy server. This \
17# script will manage the initiation of the \
18# server and it's process state.
19#
20# processname: nginx
21# config: /usr/local/nginx/conf/nginx.conf
22# pidfile: /usr/local/nginx/logs/nginx.pid
23# Provides: nginx
24#
25# Author: Jason Giedymin
26# <jason.giedymin AT gmail.com>.
27#
28# Version: 3.0 22-APR-2013 jason.giedymin AT gmail.com
29# Notes: nginx init.d dash script for Ubuntu.
30# Tested with: Ubuntu 12.10, nginx-1.3.16
31#
32# This script's project home is:
33# http://github.com/JasonGiedymin/nginx-init-ubuntu
34#
35#------------------------------------------------------------------------------
36# MIT X11 License
37#------------------------------------------------------------------------------
38#
39# Copyright (c) 2008-2013 Jason Giedymin, http://jasongiedymin.com
40#
41# Permission is hereby granted, free of charge, to any person obtaining
42# a copy of this software and associated documentation files (the
43# "Software"), to deal in the Software without restriction, including
44# without limitation the rights to use, copy, modify, merge, publish,
45# distribute, sublicense, and/or sell copies of the Software, and to
46# permit persons to whom the Software is furnished to do so, subject to
47# the following conditions:
48#
49# The above copyright notice and this permission notice shall be
50# included in all copies or substantial portions of the Software.
51#
52# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
53# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
55# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
56# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
57# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
58# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59#------------------------------------------------------------------------------
60
61#------------------------------------------------------------------------------
62# Functions
63#------------------------------------------------------------------------------
64. /lib/lsb/init-functions
65
66#------------------------------------------------------------------------------
67# Consts
68#------------------------------------------------------------------------------
69PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
70DAEMON=/usr/local/nginx/sbin/nginx
71
72PS="nginx"
73PIDNAME="nginx" #lets you do $PS-slave
74PIDFILE=$PIDNAME.pid #pid file
75PIDSPATH=/var/run/ #default pid location, you should change it
76
77DESCRIPTION="Nginx Server..."
78
79RUNAS=root #user to run as
80
81SCRIPT_OK=0 #ala error codes
82SCRIPT_ERROR=1 #ala error codes
83TRUE=1 #boolean
84FALSE=0 #boolean
85
86lockfile=/var/lock/subsys/nginx
87NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
88
89#------------------------------------------------------------------------------
90# Simple Tests
91#------------------------------------------------------------------------------
92
93#test if nginx is a file and executable
94test -x $DAEMON || exit 0
95
96# Include nginx defaults if available
97if [ -f /etc/default/nginx ] ; then
98 . /etc/default/nginx
99fi
100
101#set exit condition
102#set -e
103
104#------------------------------------------------------------------------------
105# Functions
106#------------------------------------------------------------------------------
107
108setFilePerms(){
109 if [ -f $PIDSPATH/$PIDFILE ]; then
110 chmod 400 $PIDSPATH/$PIDFILE
111 fi
112}
113
114configtest() {
115 $DAEMON -t -c $NGINX_CONF_FILE
116}
117
118getPSCount() {
119 return `pgrep -f $PS | wc -l`
120}
121
122isRunning() {
123 if [ $1 ]; then
124 pidof_daemon $1
125 PID=$?
126
127 if [ $PID -gt 0 ]; then
128 return 1
129 else
130 return 0
131 fi
132 else
133 pidof_daemon
134 PID=$?
135
136 if [ $PID -gt 0 ]; then
137 return 1
138 else
139 return 0
140 fi
141 fi
142}
143
144#courtesy of php-fpm
145wait_for_pid () {
146 try=0
147
148 while test $try -lt 35 ; do
149 case "$1" in
150 'created')
151 if [ -f "$2" ] ; then
152 try=''
153 break
154 fi
155 ;;
156
157 'removed')
158 if [ ! -f "$2" ] ; then
159 try=''
160 break
161 fi
162 ;;
163 esac
164
165 try=`expr $try + 1`
166 sleep 1
167 done
168}
169
170status(){
171 isRunning
172 isAlive=$?
173
174 if [ "${isAlive}" -eq $TRUE ]; then
175 echo "$PIDNAME found running with processes: `pidof $PS`"
176 else
177 echo "$PIDNAME is NOT running."
178 fi
179}
180
181removePIDFile(){
182 if [ $1 ]; then
183 if [ -f $1 ]; then
184 rm -f $1
185 fi
186 else
187 #Do default removal
188 if [ -f $PIDSPATH/$PIDFILE ]; then
189 rm -f $PIDSPATH/$PIDFILE
190 fi
191 fi
192}
193
194start() {
195 log_daemon_msg "Starting $DESCRIPTION"
196
197 isRunning
198 isAlive=$?
199
200 if [ "${isAlive}" -eq $TRUE ]; then
201 log_end_msg $SCRIPT_ERROR
202 else
203 start-stop-daemon --start --quiet --chuid \
204 $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
205 -- -c $NGINX_CONF_FILE
206 setFilePerms
207 log_end_msg $SCRIPT_OK
208 fi
209}
210
211stop() {
212 log_daemon_msg "Stopping $DESCRIPTION"
213
214 isRunning
215 isAlive=$?
216
217 if [ "${isAlive}" -eq $TRUE ]; then
218 start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE
219
220 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
221
222 if [ -n "$try" ] ; then
223 log_end_msg $SCRIPT_ERROR
224 else
225 removePIDFile
226 log_end_msg $SCRIPT_OK
227 fi
228 else
229 log_end_msg $SCRIPT_ERROR
230 fi
231}
232
233reload() {
234 configtest || return $?
235
236 log_daemon_msg "Reloading (via HUP) $DESCRIPTION"
237
238 isRunning
239
240 if [ $? -eq $TRUE ]; then
241 `killall -HUP $PS` #to be safe
242 log_end_msg $SCRIPT_OK
243 else
244 log_end_msg $SCRIPT_ERROR
245 fi
246}
247
248quietupgrade() {
249 log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"
250
251 isRunning
252 isAlive=$?
253
254 if [ "${isAlive}" -eq $TRUE ]; then
255 kill -USR2 `cat $PIDSPATH/$PIDFILE`
256 kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`
257
258 isRunning
259 isAlive=$?
260
261 if [ "${isAlive}" -eq $TRUE ]; then
262 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
263 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
264 removePIDFile $PIDSPATH/$PIDFILE.oldbin
265
266 log_end_msg $SCRIPT_OK
267 else
268 log_end_msg $SCRIPT_ERROR
269
270 log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"
271
272 kill -HUP `cat $PIDSPATH/$PIDFILE`
273 kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
274 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
275
276 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
277 removePIDFile $PIDSPATH/$PIDFILE.oldbin
278
279 log_end_msg $SCRIPT_ok
280 fi
281 else
282 log_end_msg $SCRIPT_ERROR
283 fi
284}
285
286terminate() {
287 log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"
288
289 PIDS=`pidof $PS` || true
290
291 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
292
293 for i in $PIDS; do
294 if [ "$i" = "$PIDS2" ]; then
295 kill $i
296 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
297 removePIDFile
298 fi
299 done
300
301 log_end_msg $SCRIPT_OK
302}
303
304destroy() {
305 log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
306 killall $PS -q >> /dev/null 2>&1
307 log_end_msg $SCRIPT_OK
308}
309
310pidof_daemon() {
311 PIDS=`pidof $PS` || true
312
313 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
314
315 for i in $PIDS; do
316 if [ "$i" = "$PIDS2" ]; then
317 return 1
318 fi
319 done
320
321 return 0
322}
323
324case "$1" in
325 start)
326 start
327 ;;
328 stop)
329 stop
330 ;;
331 restart|force-reload)
332 stop
333 sleep 1
334 start
335 ;;
336 reload)
337 $1
338 ;;
339 status)
340 status
341 ;;
342 configtest)
343 $1
344 ;;
345 quietupgrade)
346 $1
347 ;;
348 terminate)
349 $1
350 ;;
351 destroy)
352 $1
353 ;;
354 *)
355 FULLPATH=/etc/init.d/$PS
356 echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
357 echo " The 'destroy' command should only be used as a last resort."
358 exit 1
359 ;;
360esac
361
362exit 0