Skip to content

Instantly share code, notes, and snippets.

@kmorey
Created January 30, 2009 16:36
Show Gist options
  • Save kmorey/55130 to your computer and use it in GitHub Desktop.
Save kmorey/55130 to your computer and use it in GitHub Desktop.
init.d script to control nginx server. (modified from apache init.d script)
#!/bin/bash
#
# nginx Startup script for nginx server
#
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: nginx
# config: /etc/httpd/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
nginx=/usr/local/nginx/sbin/nginx
prog=nginx
pidfile=${PIDFILE-/var/run/nginx.pid}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
RETVAL=0
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
daemon $nginx $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
echo -n $"Stopping $prog: "
killproc -d 10 $nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! $nginx $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc $nginx -HUP
RETVAL=$?
fi
echo
}
test() {
echo $"Testing $prog config: "
$nginx -t
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $nginx
RETVAL=$?
;;
restart)
stop
start
;;
test)
test
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment