Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Last active September 24, 2023 23:01
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save leplatrem/5684206 to your computer and use it in GitHub Desktop.
Save leplatrem/5684206 to your computer and use it in GitHub Desktop.
gunicorn virtualenv init.d script (could be simpler with upstart)
#! /bin/bash
### BEGIN INIT INFO
# Provides: yourapp
# Required-Start: nginx
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The main django process
# Description: The gunicorn process that receives HTTP requests
# from nginx
#
### END INIT INFO
#
# Author: mle <geobi@makina-corpus.net>
#
APPNAME=yourapp
USER=youruser
PATH=/bin:/usr/bin:/sbin:/usr/sbin
ACTIVATE=/home/www/project/env/bin/activate
APPMODULE=yourproject.wsgi:application
DAEMON=gunicorn
BIND=127.0.0.1:8000
PIDFILE=/var/run/gunicorn.pid
LOGFILE=/var/log/$DAEMON.log
WORKERS=2
. /lib/lsb/init-functions
if [ -e "/etc/default/$APPNAME" ]
then
. /etc/default/$APPNAME
fi
case "$1" in
start)
log_daemon_msg "Starting deferred execution scheduler" "$APPNAME"
source $ACTIVATE
$DAEMON --daemon --bind=$BIND --pid=$PIDFILE --workers=$WORKERS --user=$USER --log-file=$LOGFILE $APPMODULE
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping deferred execution scheduler" "APPNAME"
killproc -p $PIDFILE $DAEMON
log_end_msg $?
;;
force-reload|restart)
$0 stop
$0 start
;;
status)
status_of_proc -p $PIDFILE $DAEMON && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$APPNAME {start|stop|restart|force-reload|status}"
exit 1
;;
esac
exit 0
@asyndrige
Copy link

Simple and useful, much better than other scripts seen here. Worked with flask+gunicorn on openwrt.

@liquiddandruff
Copy link

Thanks a lot!!

@racitup
Copy link

racitup commented Oct 24, 2017

For anyone wanting something like this on Ubuntu 16.04, systemd is the way to go!
I think it would be fairly trivial to convert this to gunicorn.. man systemd.service is your friend.

[Unit]
Description=waitress wsgi app server for django
After=nginx.target

[Service]
Type=simple
PermissionsStartOnly=true
User=django
ExecStart=/home/django/django_env.sh waitress-serve --unix-socket=/tmp/waitress.sock --unix-socket-perms=660 --threads=4 myproj.wsgi:application
TimeoutStartSec=20
TimeoutStopSec=10
KillMode=process
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

@Nadav-Ruskin
Copy link

Looks very promising, thank you.

@renatoaloi
Copy link

Thanks for this script!

In time I'd add a PYTHONPATH env var otherwise the gunicorn doesn't find the module nor wsgi file.

For me it is firing this error:
ModuleNotFoundError: No module named 'agenda'

So i've managed to add the following line to your script:

[...]
LOGFILE=/var/log/$DAEMON.log
WORKERS=2
PYTHONPATH=/path/where/myapp/resides
[...]

It solved the problem. Hope it helps for someone else.

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