Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created August 21, 2011 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jordanorelli/1161075 to your computer and use it in GitHub Desktop.
Save jordanorelli/1161075 to your computer and use it in GitHub Desktop.
nginx init.d script
#!/usr/bin/env bash
# Adapted from the book "Nginx HTTP Server", by Clement Nedelcu.
# Original Author: Ryuan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement Nedelcu http://cnedelcu.blogspot.com/
# Modified: Jordan Orelli http://jordanorelli.com/
# source: https://gist.github.com/1161075
# Corresponds with the following compile-time options:
# --sbin-path=/usr/local/sbin
# --pid-path=/var/run/nginx.pid
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: false
# Short-Description: start/stop nginx web server
### END INIT INFO
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME="nginx"
PIDFILE="/var/run/$NAME.pid"
DAEMON="/usr/local/sbin/$NAME"
SCRIPTNAME="/etc/init.d/$NAME"
# If the daemon file is not found, terminate the script.
if [[ ! -x "$DAEMON" ]] ; then
echo "ERROR: Unable to find nginx daemon file expected to be found"\
"at $DAEMON." >&2
exit 2
fi
d_start() {
if [[ -f "$PIDFILE" ]] ; then
echo "ERROR: $NAME appears to be running. Check $PIDFILE" >&2
exit 4
fi
"$DAEMON"
}
d_stop() {
if [[ ! -f "$PIDFILE" ]] ; then
echo "ERROR: $NAME does not appear to be running; pid file $PIDFILE not found." >&2
exit 5
fi
$DAEMON -s quit
}
d_reload() {
if [[ -f "$PIDFILE" ]] ; then
"$DAEMON" -s reload
else
"$DAEMON"
fi
}
d_status() {
if [[ -f "$PIDFILE" ]] ; then
echo "$NAME is running."
else
echo "$NAME is not running."
fi
}
d_info() {
"$DAEMON" -V
}
case "$1" in
start)
echo "Starting $DESC: $NAME"
d_start
;;
stop)
echo "Stopping $DESC: $NAME"
d_stop
;;
reload | force-reload)
echo "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds to give the nginx daemon some time to perform
# a graceful stop.
sleep 2
d_start
;;
status)
d_status
;;
info)
d_info
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload|info}" >&2
exit 3
;;
esac
exit 0
@jordanorelli
Copy link
Author

after picking up the book "Nginx HTTP Server" and compiling nginx from source, I found that none of the init.d scripts I was finding were to my liking, so I updated the one from the book.

  • using /usr/sbin/env bash instead of /bin/sh, which may be a shitty idea for startup scripts but I'm not really sure.
  • added the "info" option to check compile-time config options.
  • added the "force-reload" and "status" options for LSB compliance.
  • changed some of the output behavior.
  • checking for pidfile before attempting to start nginx when it's already running
  • checking for pidfile before attempting to stop nginx when it's not running
  • added LSB init info block.

I've only tested this on Debian, but it appears to work just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment