· 6 years ago · Aug 14, 2019, 01:08 AM
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.9.0 12-MAY-2015 jason.giedymin AT gmail.com
29# Notes: nginx init.d dash script for Ubuntu.
30# Tested with: Ubuntu 14.10, nginx-1.7.9
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#------------------------------------------------------------------------------
64LSB_FUNC=/lib/lsb/init-functions
65
66# Test that init functions exists
67test -r $LSB_FUNC || {
68 echo "$0: Cannot find $LSB_FUNC! Script exiting." 1>&2
69 exit 5
70}
71
72. $LSB_FUNC
73
74#------------------------------------------------------------------------------
75# Consts
76#------------------------------------------------------------------------------
77# Include nginx defaults if available
78if [ -f /etc/default/nginx ]; then
79 . /etc/default/nginx
80fi
81
82# Minimize path
83PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
84
85PS=${PS:-"nginx"} # process name
86DESCRIPTION=${DESCRIPTION:-"NginX Streaming Server"} # process description
87NGINXPATH=${NGINXPATH:-/etc/nginx} # root path where installed
88DAEMON=${DAEMON:-/usr/sbin/nginx} # path to daemon binary
89NGINX_CONF_FILE=${NGINX_CONF_FILE:-$NGINXPATH/nginx.conf} # config file path
90
91PIDNAME=${PIDNAME:-"nginx"} # lets you do $PS-slave
92PIDFILE=${PIDFILE:-$PIDNAME.pid} # pid file
93PIDSPATH=${PIDSPATH:-/var/log/nginx} # default pid location, you should change it
94RUNAS=${RUNAS:-root} # user to run as
95
96SCRIPT_OK=0 # ala error codes
97SCRIPT_ERROR=1 # ala error codes
98TRUE=1 # boolean
99FALSE=0 # boolean
100
101#------------------------------------------------------------------------------
102# Simple Tests
103#------------------------------------------------------------------------------
104
105# Test if nginx is a file and executable
106test -x $DAEMON || {
107 echo "$0: You don't have permissions to execute nginx." 1>&2
108 exit 4
109}
110
111# You can also set your conditions like so:
112# set exit condition
113# set -e
114
115#------------------------------------------------------------------------------
116# Functions
117#------------------------------------------------------------------------------
118
119setFilePerms(){
120 if [ -f $PIDSPATH/$PIDFILE ]; then
121 chmod 400 $PIDSPATH/$PIDFILE
122 fi
123}
124
125configtest() {
126 $DAEMON -t -c $NGINX_CONF_FILE
127}
128
129getPSCount() {
130 return `pgrep -f $PS | wc -l`
131}
132
133isRunning() {
134 if [ $1 ]; then
135 pidof_daemon $1
136 PID=$?
137
138 if [ $PID -gt 0 ]; then
139 return 1
140 else
141 return 0
142 fi
143 else
144 pidof_daemon
145 PID=$?
146
147 if [ $PID -gt 0 ]; then
148 return 1
149 else
150 return 0
151 fi
152 fi
153}
154
155#courtesy of php-fpm
156wait_for_pid () {
157 try=0
158
159 while test $try -lt 35 ; do
160 case "$1" in
161 'created')
162 if [ -f "$2" ]; then
163 try=''
164 break
165 fi
166 ;;
167
168 'removed')
169 if [ ! -f "$2" ]; then
170 try=''
171 break
172 fi
173 ;;
174 esac
175
176 try=`expr $try + 1`
177 sleep 1
178 done
179}
180
181status(){
182 isRunning
183 isAlive=$?
184
185 if [ "${isAlive}" -eq $TRUE ]; then
186 log_warning_msg "$DESCRIPTION found running with processes: `pidof $PS`"
187 rc=0
188 else
189 log_warning_msg "$DESCRIPTION is NOT running."
190 rc=3
191 fi
192
193 return
194}
195
196removePIDFile(){
197 if [ $1 ]; then
198 if [ -f $1 ]; then
199 rm -f $1
200 fi
201 else
202 #Do default removal
203 if [ -f $PIDSPATH/$PIDFILE ]; then
204 rm -f $PIDSPATH/$PIDFILE
205 fi
206 fi
207}
208
209start() {
210 log_daemon_msg "Starting $DESCRIPTION"
211
212 isRunning
213 isAlive=$?
214
215 if [ "${isAlive}" -eq $TRUE ]; then
216 log_end_msg $SCRIPT_ERROR
217 rc=0
218 else
219 start-stop-daemon --start --quiet --chuid \
220 $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
221 -- -c $NGINX_CONF_FILE
222 status=$?
223 setFilePerms
224
225 if [ "${status}" -eq 0 ]; then
226 log_end_msg $SCRIPT_OK
227 rc=0
228 else
229 log_end_msg $SCRIPT_ERROR
230 rc=7
231 fi
232 fi
233
234 return
235}
236
237stop() {
238 log_daemon_msg "Stopping $DESCRIPTION"
239
240 isRunning
241 isAlive=$?
242
243 if [ "${isAlive}" -eq $TRUE ]; then
244 start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE
245
246 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
247
248 if [ -n "$try" ]; then
249 log_end_msg $SCRIPT_ERROR
250 rc=0 # lsb states 1, but under status it is 2 (which is more prescriptive). Deferring to standard.
251 else
252 removePIDFile
253 log_end_msg $SCRIPT_OK
254 rc=0
255 fi
256 else
257 log_end_msg $SCRIPT_ERROR
258 rc=7
259 fi
260
261 return
262}
263
264reload() {
265 configtest || return $?
266
267 log_daemon_msg "Reloading (via HUP) $DESCRIPTION"
268
269 isRunning
270
271 if [ $? -eq $TRUE ]; then
272 kill -HUP `cat $PIDSPATH/$PIDFILE`
273 log_end_msg $SCRIPT_OK
274 rc=0
275 else
276 log_end_msg $SCRIPT_ERROR
277 rc=7
278 fi
279
280 return
281}
282
283quietupgrade() {
284 log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"
285
286 isRunning
287 isAlive=$?
288
289 if [ "${isAlive}" -eq $TRUE ]; then
290 kill -USR2 `cat $PIDSPATH/$PIDFILE`
291 kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`
292
293 isRunning
294 isAlive=$?
295
296 if [ "${isAlive}" -eq $TRUE ]; then
297 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
298 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
299 removePIDFile $PIDSPATH/$PIDFILE.oldbin
300
301 log_end_msg $SCRIPT_OK
302 rc=0
303 else
304 log_end_msg $SCRIPT_ERROR
305
306 log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"
307
308 kill -HUP `cat $PIDSPATH/$PIDFILE`
309 kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
310 kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
311
312 wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
313 removePIDFile $PIDSPATH/$PIDFILE.oldbin
314
315 log_end_msg $SCRIPT_OK
316 rc=0
317 fi
318 else
319 log_end_msg $SCRIPT_ERROR
320 rc=7
321 fi
322
323 return
324}
325
326terminate() {
327 log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"
328
329 PIDS=`pidof $PS` || true
330
331 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
332
333 for i in $PIDS; do
334 if [ "$i" = "$PIDS2" ]; then
335 kill $i
336 wait_for_pid 'removed' $PIDSPATH/$PIDFILE
337 removePIDFile
338 fi
339 done
340
341 log_end_msg $SCRIPT_OK
342 rc=0
343}
344
345destroy() {
346 log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
347 killall $PS -q >> /dev/null 2>&1
348 log_end_msg $SCRIPT_OK
349 rc=0
350}
351
352pidof_daemon() {
353 PIDS=`pidof $PS` || true
354
355 [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
356
357 for i in $PIDS; do
358 if [ "$i" = "$PIDS2" ]; then
359 return 1
360 fi
361 done
362
363 return 0
364}
365
366action="$1"
367case "$1" in
368 start)
369 start
370 ;;
371 stop)
372 stop
373 ;;
374 restart|force-reload)
375 stop
376 # if [ $rc -ne 0 ]; then
377 # script_exit
378 # fi
379 sleep 1
380 start
381 ;;
382 reload)
383 $1
384 ;;
385 status)
386 status
387 ;;
388 configtest)
389 $1
390 ;;
391 quietupgrade)
392 $1
393 ;;
394 terminate)
395 $1
396 ;;
397 destroy)
398 $1
399 ;;
400 *)
401 FULLPATH=/etc/init.d/$PS
402 echo "Usage: $FULLPATH {start|stop|restart|force-reload|reload|status|configtest|quietupgrade|terminate|destroy}"
403 echo " The 'destroy' command should only be used as a last resort."
404 exit 3
405 ;;
406esac
407
408exit $rc