Skip to content

Instantly share code, notes, and snippets.

@hoov
Created March 30, 2016 16:51
Show Gist options
  • Save hoov/900f83536d07f1ea4da3eec0bcd92b8c to your computer and use it in GitHub Desktop.
Save hoov/900f83536d07f1ea4da3eec0bcd92b8c to your computer and use it in GitHub Desktop.
#!/bin/sh
### BEGIN INIT INFO
# Provides: gunicorn
# 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
# Short-Description: starts the gunicorn web server
# Description: starts gunicorn using start-stop-daemon
### END INIT INFO
DAEMON=/usr/local/bin/gunicorn
IS_ENGINE=/usr/local/insightsquared/is_engine
RUN_DOT_PY=$IS_ENGINE/bbase/bin/run.py
NAME=python
DISPLAY_NAME=gunicorn
DESC=gunicorn
# Include gunicorn defaults if available
if [ -r /etc/default/gunicorn ]; then
. /etc/default/gunicorn
fi
STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
test -x $DAEMON || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
PID=/run/gunicorn.pid
# Check if the ULIMIT is set in /etc/default/gunicorn
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
CMD="$DAEMON bbase.wsgi_handler:application --daemon --pid $PID -c $IS_ENGINE/bbase/gunicorn_conf.py"
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --chdir $IS_ENGINE --pidfile $PID --exec $RUN_DOT_PY --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --chdir $IS_ENGINE --pidfile $PID --exec $RUN_DOT_PY -- \
$CMD 2>/dev/null \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep 1
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
return 0
}
#
# Rotate log files
#
do_rotate() {
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return 0
}
#
# Online upgrade gunicorn executable
#
# "Upgrading to a new binary on the fly"
# http://docs.gunicorn.org/en/stable/signals.html#upgrading-to-a-new-binary-on-the-fly
#
do_upgrade() {
# Return
# 0 if gunicorn has been successfully upgraded
# 1 if gunicorn is not running
# 2 if the pid files were not created on time
# 3 if the old master could not be killed
if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
# Wait for both old and new master to write their pid file
while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
cnt=`expr $cnt + 1`
if [ $cnt -gt 10 ]; then
return 2
fi
sleep 1
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return 0
else
return 3
fi
else
return 1
fi
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$DISPLAY_NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$DISPLAY_NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$DISPLAY_NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration" "$DISPLAY_NAME"
do_reload
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$DISPLAY_NAME" && exit 0 || exit $?
;;
upgrade)
log_daemon_msg "Upgrading binary" "$DISPLAY_NAME"
do_upgrade
log_end_msg 0
;;
rotate)
log_daemon_msg "Re-opening $DESC log files" "$DISPLAY_NAME"
do_rotate
log_end_msg $?
;;
*)
echo "Usage: $DISPLAY_NAME {start|stop|restart|reload|force-reload|status|rotate|upgrade}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment