ubuntu系统中nginx启动脚本
#! /bin/sh
### begin init info
# provides: nginx
# required-start: $remote_fs $syslog
# required-stop: $remote_fs $syslog
# default-start: 2 3 4 5
# default-stop: 0 1 6
# short-description: nginx init.d dash script for ubuntu or other *nix.
# description: nginx init.d dash script for ubuntu or other *nix.
### end init info
#------------------------------------------------------------------------------
# nginx - this debian almquist shell (dash) script, starts and stops the nginx
# daemon for ubuntu and other *nix releases.
#
# description: nginx is an http(s) server, http(s) reverse \
# proxy and imap/pop3 proxy server. this \
# script will manage the initiation of the \
# server and it's process state.
#
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# provides: nginx
#
# author: jason giedymin
# <jason.giedymin at gmail.com>.
#
# version: 3.5.1 11-nov-2013 jason.giedymin at gmail.com
# notes: nginx init.d dash script for ubuntu.
# tested with: ubuntu 13.10, nginx-1.4.3
#
# this script's project home is:
# http://github.com/jasongiedymin/nginx-init-ubuntu
#
#------------------------------------------------------------------------------
# mit x11 license
#------------------------------------------------------------------------------
#
# copyright (c) 2008-2013 jason giedymin, http://jasongiedymin.com
#
# permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "software"), to deal in the software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the software, and to
# permit persons to whom the software is furnished to do so, subject to
# the following conditions:
#
# the above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the software.
#
# the software is provided "as is", without warranty of any kind,
# express or implied, including but not limited to the warranties of
# merchantability, fitness for a particular purpose and
# noninfringement. in no event shall the authors or copyright holders be
# liable for any claim, damages or other liability, whether in an action
# of contract, tort or otherwise, arising from, out of or in connection
# with the software or the use or other dealings in the software.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------
lsb_func=/lib/lsb/init-functions
# test that init functions exists
test -r $lsb_func || {
echo "$0: cannot find $lsb_func! script exiting." 1>&2
exit 5
}
. $lsb_func
#------------------------------------------------------------------------------
# consts
#------------------------------------------------------------------------------
path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
daemon=/usr/local/nginx/sbin/nginx
ps="nginx"
pidname="nginx" #lets you do $ps-slave
pidfile=$pidname.pid #pid file
pidspath=/usr/local/nginx/logs #default pid location, you should change it
description="nginx server..."
runas=root #user to run as
script_ok=0 #ala error codes
script_error=1 #ala error codes
true=1 #boolean
false=0 #boolean
lockfile=/var/lock/subsys/nginx
nginx_conf_file="/usr/local/nginx/conf/nginx.conf"
#------------------------------------------------------------------------------
# simple tests
#------------------------------------------------------------------------------
# test if nginx is a file and executable
test -x $daemon || {
echo "$0: you don't have permissions to execute nginx." 1>&2
exit 4
}
# include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi
#set exit condition
#set -e
#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------
setfileperms(){
if [ -f $pidspath/$pidfile ]; then
chmod 400 $pidspath/$pidfile
fi
}
configtest() {
$daemon -t -c $nginx_conf_file
}
getpscount() {
return `pgrep -f $ps | wc -l`
}
isrunning() {
if [ $1 ]; then
pidof_daemon $1
pid=$?
if [ $pid -gt 0 ]; then
return 1
else
return 0
fi
else
pidof_daemon
pid=$?
if [ $pid -gt 0 ]; then
return 1
else
return 0
fi
fi
}
#courtesy of php-fpm
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ]; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ]; then
try=''
break
fi
esac
try=`expr $try + 1`
sleep 1
done
}
status(){
isrunning
isalive=$?
if [ "${isalive}" -eq $true ]; then
log_warning_msg "$description found running with processes: `pidof $ps`"
rc=0
else
log_warning_msg "$description is not running."
rc=3
fi
return
}
removepidfile(){
if [ $1 ]; then
if [ -f $1 ]; then
rm -f $1
fi
else
#do default removal
if [ -f $pidspath/$pidfile ]; then
rm -f $pidspath/$pidfile
fi
fi
}
start() {
log_daemon_msg "starting $description"
isrunning
isalive=$?
if [ "${isalive}" -eq $true ]; then
log_end_msg $script_error
rc=0
else
start-stop-daemon --start --quiet --chuid \
$runas --pidfile $pidspath/$pidfile --exec $daemon \
-- -c $nginx_conf_file
setfileperms
log_end_msg $script_ok
rc=0
fi
return
}
stop() {
log_daemon_msg "stopping $description"
isrunning
isalive=$?
if [ "${isalive}" -eq $true ]; then
start-stop-daemon --stop --quiet --pidfile $pidspath/$pidfile
wait_for_pid 'removed' $pidspath/$pidfile
if [ -n "$try" ]; then
log_end_msg $script_error
rc=0 # lsb states 1, but under status it is 2 (which is more prescriptive). deferring to standard.
else
removepidfile
log_end_msg $script_ok
rc=0
fi
else
log_end_msg $script_error
rc=7
fi
return
}
reload() {
configtest || return $?
log_daemon_msg "reloading (via hup) $description"
isrunning
if [ $? -eq $true ]; then
kill -hup `cat $pidspath/$pidfile`
log_end_msg $script_ok
rc=0
else
log_end_msg $script_error
rc=7
fi
return
}
quietupgrade() {
log_daemon_msg "peforming quiet upgrade $description"
isrunning
isalive=$?
if [ "${isalive}" -eq $true ]; then
kill -usr2 `cat $pidspath/$pidfile`
kill -winch `cat $pidspath/$pidfile.oldbin`
isrunning
isalive=$?
if [ "${isalive}" -eq $true ]; then
kill -quit `cat $pidspath/$pidfile.oldbin`
wait_for_pid 'removed' $pidspath/$pidfile.oldbin
removepidfile $pidspath/$pidfile.oldbin
log_end_msg $script_ok
rc=0
else
log_end_msg $script_error
log_daemon_msg "error! reverting back to original $description"
kill -hup `cat $pidspath/$pidfile`
kill -term `cat $pidspath/$pidfile.oldbin`
kill -quit `cat $pidspath/$pidfile.oldbin`
wait_for_pid 'removed' $pidspath/$pidfile.oldbin
removepidfile $pidspath/$pidfile.oldbin
log_end_msg $script_ok
rc=0
fi
else
log_end_msg $script_error
rc=7
fi
return
}
terminate() {
log_daemon_msg "force terminating (via kill) $description"
pids=`pidof $ps` || true
[ -e $pidspath/$pidfile ] && pids2=`cat $pidspath/$pidfile`
for i in $pids; do
if [ "$i" = "$pids2" ]; then
kill $i
wait_for_pid 'removed' $pidspath/$pidfile
removepidfile
fi
done
log_end_msg $script_ok
rc=0
}
destroy() {
log_daemon_msg "force terminating and may include self (via killall) $description"
killall $ps -q >> /dev/null 2>&1
log_end_msg $script_ok
rc=0
}
pidof_daemon() {
pids=`pidof $ps` || true
[ -e $pidspath/$pidfile ] && pids2=`cat $pidspath/$pidfile`
for i in $pids; do
if [ "$i" = "$pids2" ]; then
return 1
fi
done
return 0
}
action="$1"
case "$1" in
start)
start
stop)
stop
restart|force-reload)
stop
# if [ $rc -ne 0 ]; then
# script_exit
# fi
sleep 1
start
reload)
$1
status)
status
configtest)
$1
quietupgrade)
$1
terminate)
$1
destroy)
$1
*)
fullpath=/etc/init.d/$ps
echo "usage: $fullpath {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
echo " the 'destroy' command should only be used as a last resort."
exit 3
esac
exit $rc