Skip to content

Instantly share code, notes, and snippets.

@tzengerink
Last active December 13, 2016 15:46
Show Gist options
  • Save tzengerink/4085602 to your computer and use it in GitHub Desktop.
Save tzengerink/4085602 to your computer and use it in GitHub Desktop.
Gunicorn Deploy Script
#!/bin/bash -e
#
# GUNICORN DEPLOY SCRIPT
# ----------------------
# Script to deploy your application with Gunicorn. It can be called by your
# process manager (eg. supervisor). Your application should use a virtual
# environment in a subdirectory.
#
# Copyright (c) 2013-2014, T. Zengerink
# Licensed under MIT License.
# See: https://gist.githubusercontent.com/Mytho/3151357/raw/LICENSE.md
BIND="127.0.0.1:8080"
GROUP="http"
USER="http"
WORKERS=5
WEBROOT="/srv/http"
VENV=".py-env"
MODULE=application:app
## -- No need to edit below this line ------------------------------------------
SUBDIR=$(echo $(pwd) | sed s#$WEBROOT/##g)
DOMAIN=$(echo $SUBDIR | cut -d \/ -f 1)
SUBDOMAIN=$(echo $SUBDIR | cut -d \/ -f 2)
PID="/run/gunicorn-$SUBDOMAIN.$DOMAIN.pid"
LOG="/var/log/gunicorn/$SUBDOMAIN.$DOMAIN-error.log"
## Functions
prepare()
{
cd $WEBROOT/$DOMAIN/$SUBDOMAIN
source $WEBROOT/$DOMAIN/$SUBDOMAIN/$VENV/bin/activate
test -d $(dirname $LOG) || mkdir -p $(dirname $LOG)
}
start()
{
gunicorn --daemon \
--pid $PID \
--user $USER \
--group $GROUP \
--workers $WORKERS \
--bind $BIND \
--log-file $LOG $MODULE 2>> $LOG
sleep 5
}
stop()
{
kill -TERM $(cat $PID)
sleep 2
}
## Execute
prepare
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 [start|stop|restart]"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment