Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gunicorn upstart template
# %(mysite)s - run %(mysite)s instances (default is the prod instance)
#
# This runs gunicorn-django for %(mysite)s; to install:
# * sudo ln -s <this file> /etc/init/%(mysite)s
# * sudo initctl reload-configuration
#
# it expects the following directory layout:
#
# /home/%(mysite)s/public_html
# \-env -> virtualenv
# | \-bin
# | \-gunicorn_django
# | \-activate
# \-<instance>
# | \-app -> application
# | \-conf
# | | \-gunicorn_<instance>.conf.py
# | | \-<this file>
# | \-log -> logs
# | \-run -> pid files and the like
# \-<instance>
# ...
#
# to use:
# * sudo start/stop/restart/reload/status %(mysite)s
# * sudo start/stop/restart/reload/status %(mysite)s instance=testing
description "%(mysite)s gunicorn instance, default is the prod instance"
start on runlevel [2345]
stop on runlevel [06]
expect daemon
respawn
respawn limit 10 5
instance ${instance:-prod}
env APPHOME="/home/%(mysite)s/public_html"
env USER="django"
pre-start script
INST=${instance:-prod}
GUNICORN="$APPHOME/env/bin/gunicorn_django"
test -n "$GUNICORN" -a -x "$GUNICORN" || { stop; exit 0; }
test -d "$APPHOME" || { stop; exit 0; }
end script
script
INST=${instance:-prod}
CONF="$APPHOME/$INST/conf/gunicorn_$INST.conf.py"
GUNICORN="$APPHOME/env/bin/gunicorn_django"
PIDFILE="$APPHOME/$INST/run/$INST.pid"
LOGFILE="$APPHOME/$INST/log/gunicorn.log"
cd "$APPHOME/$INST/app"
exec $GUNICORN --pid="$PIDFILE" --name="$INST" --user="$USER" --group="$USER" --config="$CONF" --daemon --log-file="$LOGFILE" 2>>"$LOGFILE"
end script
post-stop script
INST=${instance:-prod}
rm -rf "$APPHOME/$INST/run/$INST".{pid,sock}
end script
# vim: set et sw=4 ts=4 sts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment