Gunicorn / Django Debian init script

This is a simple Debian init.d script for my self-hosted Django/Gunicorn websites. Note that it runs inside a virtualenv (my installed Django versions are different in each virtualenv). If you have questions: comment this post!

#!/bin/sh 

### BEGIN INIT INFO
# Provides:       seismic_web
# 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 seismic_web
### END INIT INFO

# www-data is the default www user on debian
USER=www-data
NAME="seismic_web"
GUNICORN_RUN="gunicorn_django"
# Confdir: the Django project inside the virtualenv
CONFDIR="/home/thomas/seismic_web/seismic_web"
VENV_ACTIVATION=". ../bin/activate"
RETVAL=0
# PID: I always name my gunicorn pidfiles this way
PID="/tmp/gunicorn_"$NAME".pid"

# source function library
. /lib/lsb/init-functions


start()
{
    echo "Starting $NAME."
    cd $CONFDIR;
    su -c "$VENV_ACTIVATION; $GUNICORN_RUN" $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

About Thomas Pelletier

17-year old student fond of math, science and IT. I like spending my spare time building websites with nice tools such as Django, Mercurial and Textmate. New technologies and web services are some of my passions. When I've something I'd like to talk about, I write a post and publish it on this website.
This entry was posted in None and tagged , . Bookmark the permalink.

7 Responses to Gunicorn / Django Debian init script

  1. Pingback: Script per arrancar un projecte gunicorn. » Blog de noveo

  2. Matt says:

    Thanks Thomas. That shouldn't be difficult, but I'm just not sure it's the right way. None of the init scripts on my Ubuntu and Debian boxes work like that (I suppose you could say cron does, but that seems a bit different). I had a cursory look through the Debian Policy Manual and Filesystem Hierarchy Standard, without learning much ;-).

    It also seems like there are several points of failure — the web server virtual host config, the gunicorn config file for that host, plus the method the init script uses to find them.

    I guess an alternative would be to nominate the directory where you store your django projects, look inside it for files named gunicorn.conf.py and then start gunicorn for each of them. That would require a gunicorn.conf.py for each project, but that's probably not a bad thing.

  3. I thought about that, but I did not succeed to choice the best syntax to use in the configuration files (and I admit I hadn't spend so much time on this problem).
    If you've got some ideas on how to implement that, I would be glad to ear them :)

  4. Matt says:

    So you need a separate init script for each Django site? Wouldn't it be nicer to have a config direcctory to identify the django sites and have the init script load each one?

    (Or is this a requirement of using virtualenv?)

  5. coulix says:

    All hail to gunicorn

  6. I install Gunicorn into the virtualenv. Here is the line which starts it up:
    su -c “$VENV_ACTIVATION; $GUNICORN_RUN” $USER
    As you can see, first I activate the virtualenv, then I run Gunicorn. So when I call gunicorn_django, it's indeed /home/thomas/seismic_web/bin/gunicorn_django which is called.

  7. I've been trying to get good instructions for using Gunicorn with virtualenv for the last week or so. The consensus I had was to install Gunicorn into the virtual env and then run it with something like “/home/thomas/seismic_web/bin/gunicorn”.

    Did you have issues with that sort of config, or do you prefer installing Gunicorn system wide for a different reason?

blog comments powered by Disqus