Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created August 20, 2011 14:45
Show Gist options
  • Save TimFletcher/1159189 to your computer and use it in GitHub Desktop.
Save TimFletcher/1159189 to your computer and use it in GitHub Desktop.
Gunicorn Startup Script
#!/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
@jbasko
Copy link

jbasko commented Jul 18, 2013

Why do you have two servers defined?

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