Skip to content

Instantly share code, notes, and snippets.

@mrsarm
Forked from TimFletcher/gunicorn_startup.sh
Last active December 16, 2015 16:09
Show Gist options
  • Save mrsarm/5460996 to your computer and use it in GitHub Desktop.
Save mrsarm/5460996 to your computer and use it in GitHub Desktop.
Startup Unix script for Gunicorn server.
#!/bin/sh
#
# /etc/init.d/gunicorn
#
# Startup script for Gunicorn server.
#
# Author: Tim Fletcher (2011)
# Modified by Mariano Ruiz <mrsarm@gmail.com> (2013)
#
# Original source:
# https://gist.github.com/TimFletcher/1159189
# This source:
# https://gist.github.com/mrsarm/5460996
#
### BEGIN INIT INFO
# Provides: gunicorn
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: Startup script for Gunicorn server.
### END INIT INFO
# Install this service with:
# insserv -d gunicorn -> SUSE
# update-rc.d gunicorn defaults -> Debian
# update-rc.d gunicorn start 20 2 3 5 . stop 20 0 1 6 . -> Debian
# chkconfig --level 235 gunicorn on -> Other distros
#
# Uninstall this service with:
# insserv -r gunicorn -> SUSE
# update-rc.d -f gunicorn remove -> Debian
# chkconfig --level 235 gunicorn off -> Other distros
ADDRESS='0.0.0.0'
GUNICORN="gunicorn_django"
PROJECTLOC="/opt/django-zoook/django_zoook"
MANAGELOC="$PROJECTLOC/manage.py"
USER=$USER
DEFAULT_ARGS="--workers=4 --daemon --user=$USER --bind=$ADDRESS:"
BASE_CMD="$GUNICORN $DEFAULT_ARGS"
SERVER1_PORT='8000'
SERVER1_PID="$PROJECTLOC/$SERVER1_PORT.pid"
SERVER2_PORT='8001'
SERVER2_PID="$PROJECTLOC/$SERVER2_PORT.pid"
start_server () {
if [ -f $1 ]; then
#pid exists, check if running
if [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "Server already running on ${ADDRESS}:${2}"
return
fi
fi
echo "Starting Gunicorn server ${ADDRESS}:${2}"
cd $PROJECTLOC
$BASE_CMD$2 --pid=$1
}
stop_server () {
if [ -f $1 ] && [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "Stopping Gunicorn server ${ADDRESS}:${2}"
kill -9 `cat $1`
rm $1
else
if [ -f $1 ]; then
echo "Gunicorn server ${ADDRESS}:${2} not running"
else
echo "No pid file found for Gunicorn server ${ADDRESS}:${2}"
fi
fi
}
status_server () {
if [ -f $1 ]; then
#pid exists, check if running
if [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "Gunicorn server already running on ${ADDRESS}:${2}"
return
fi
fi
echo "Gunicorn server ${ADDRESS}:${2} not running"
}
case "$1" in
'start')
start_server $SERVER1_PID $SERVER1_PORT
start_server $SERVER2_PID $SERVER2_PORT
;;
'stop')
stop_server $SERVER1_PID $SERVER1_PORT
stop_server $SERVER2_PID $SERVER2_PORT
;;
'restart')
stop_server $SERVER1_PID $SERVER1_PORT
sleep 2
start_server $SERVER1_PID $SERVER1_PORT
sleep 2
stop_server $SERVER2_PID $SERVER2_PORT
sleep 2
start_server $SERVER2_PID $SERVER2_PORT
;;
'status')
status_server $SERVER1_PID $SERVER1_PORT
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment