Skip to content

Instantly share code, notes, and snippets.

@m0n5t3r
Created July 30, 2010 12:27
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save m0n5t3r/500414 to your computer and use it in GitHub Desktop.
Save m0n5t3r/500414 to your computer and use it in GitHub Desktop.
template for a gunicorn upstart job that can run several instances of a django application
# %(mysite)s - run %(mysite)s instances (default is the main production 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 main instance"
start on runlevel [2345]
stop on runlevel [06]
expect fork
respawn
respawn limit 10 5
instance ${instance:-main}
env APPHOME="/home/%(mysite)s/public_html"
pre-start script
INST=${instance:-main}
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:-main}
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"
source "$APPHOME/env/bin/activate"
exec $GUNICORN --pid="$PIDFILE" --name="$INST" --user=django --group=django --config="$CONF" --daemon --log-file="$LOGFILE" 2>>"$LOGFILE"
end script
post-stop script
INST=${instance:-main}
rm -rf "$APPHOME/$INST/run/$INST".{pid,sock}
end script
# vim: set et sw=4 ts=4 sts=4
@jacobg
Copy link

jacobg commented Jul 25, 2012

I don't think "--daemon" should be set in upstart, because you don't want to detach it from the upstart service. The daemon option is used when you run gunicorn_django directly from the command line.

@lechup
Copy link

lechup commented Jun 5, 2013

Things to mention:

  • Upstart in ubuntu 12.04 uses /bin/sh in script section, so source is . in /bin/sh
  • Do we really need expect fork and --daemon in gunicorn part?

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