Simple Debian Init Script for Gunicorn
July 30, 2010
Here's a simple Debian init script for starting and stopping a gunicorn server. This is very basic, but it serves my purposes. The key improvement to make would probably be moving the config out of the init script and into a config file.
The values that need changing to get this to work for any application are CONFDIR, NAME and PORT. Change WORKERS if you want to start more workers for this process. This script makes the assumption that you have a top level directory (CONFDIR) which includes all the required suppliers (ie django, south etc). This directory contains your django project in a folder called NAME, which has a settings file called settingslive.py. Therefore the full path to settingslive.py is $CONFDIR/$NAME/settingslive.py.
#!/bin/sh ### BEGIN INIT INFO # Provides: drumcoder # Required-Start: $local_fs $syslog # Required-Stop: $local_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Gunicorn processes for drumcoder ### END INIT INFO CONFDIR="/home/tjs/web/drumcoder" NAME="drumcoder" PORT=8001 WORKERS=1 USER=www-data GUNICORN_RUN="gunicorn_django" PID="/tmp/gunicorn_"$NAME".pid" RETVAL=0 # source function library . /lib/lsb/init-functions start() { echo "Starting $NAME." export PYTHONPATH=$CONFDIR; cd $CONFDIR/$NAME; su -c "$GUNICORN_RUN -b localhost:$PORT -n $NAME -w $WORKERS -p $PID -D settingslive.py" $USER && echo "OK" || echo "failed"; } stop() { echo "Stopping $NAME" kill -QUIT `cat $PID` && echo "OK" || echo "failed"; } reload() { echo "Reloading $NAME:" if [ -f $PID ] then kill -HUP `cat $PID` && echo "OK" || echo "failed"; fi } case "$1" in start) start ;; stop) stop ;; restart) reload ;; reload) reload ;; force-reload) stop && start ;; *) echo $"Usage: $0 {start|stop|restart}" RETVAL=1 esac exit $RETVAL
References
This script is based on the one found at http://thomas.pelletier.im/2010/05/gunicorn-django-debian-init-script/, but I'm not using virtualenv or a seperate gunicorn config file. See also http://drumcoder.co.uk/blog/2011/jul/20/gunicorn-initd-script-virtualenv/ for a version that makes use of VirtualEnv.