Skip to content

Instantly share code, notes, and snippets.

@ekinertac
Forked from TimFletcher/gunicorn_startup.sh
Created July 8, 2013 01:06
Show Gist options
  • Save ekinertac/5945576 to your computer and use it in GitHub Desktop.
Save ekinertac/5945576 to your computer and use it in GitHub Desktop.
#!/bin/sh
# http://nathanvangheem.com/news/gunicorn-startup-script-for-django
# Place the script in the file - /etc/init.d/gunicorn or whatever you'd like to call it
# make it executable - chmod +x /etc/init.d/gunicorn
# And finally, wire it up - update-rc.d gunicorn defaults
ADDRESS='127.0.0.1'
PYTHON="/opt/django/bin/python"
GUNICORN="/opt/django/bin/gunicorn_django"
PROJECTLOC="/opt/django/project"
MANAGELOC="$PROJECTLOC/manage.py"
DEFAULT_ARGS="--workers=3 --daemon --bind=$ADDRESS:"
BASE_CMD="$GUNICORN $DEFAULT_ARGS"
SERVER1_PORT='8200'
SERVER1_PID="$PROJECTLOC/$SERVER1_PORT.pid"
SERVER2_PORT='8201'
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
cd $PROJECTLOC
echo "starting ${ADDRESS}:${2}"
$BASE_CMD$2 --pid=$1
}
stop_server (){
if [ -f $1 ] && [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "stopping server ${ADDRESS}:${2}"
kill -9 `cat $1`
rm $1
else
if [ -f $1 ]; then
echo "server ${ADDRESS}:${2} not running"
else
echo "No pid file found for server ${ADDRESS}:${2}"
fi
fi
}
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
;;
*)
echo "Usage: $0 { start | stop | restart }"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment