Skip to content

Instantly share code, notes, and snippets.

@mattseymour
Last active August 14, 2019 06:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mattseymour/5174229f3d0281f35f1e to your computer and use it in GitHub Desktop.
Debian gunicorn init.d script
#!/bin/sh
### BEGIN INIT INFO
# Provides: webapp
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: gunicorn init.d script for ubuntu and debian
# Description: gunicorn init.d script for running gunicorn on ubuntu or debian based systems
# start script using `/etc/init.d/webapp start`
### END INIT INFO
# Author: Matt Seymour <mattaseymour@gmail.com>
ADDRESS='127.0.0.1'
PYTHON="/home/test/.env/targetting/bin/python"
GUNICORN="/home/test/.env/targetting/bin/gunicorn"
PROJECTDIR="/home/test/webapp"
SERVER_PROJECTID='8000'
SERVER_PID="$PROJECTDIR/$SERVER_PROJECTID.pid"
start_server () {
if [ -f $1 ]; then
#pid exists, check if running
if [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "Server already running at ${ADDRESS}:${2}"
return
fi
fi
echo "starting ${ADDRESS}:${2}"
$GUNICORN -c $PROJECTDIR/gunicorn_settings.py -p $SERVER_PID wsgi:application
}
stop_server (){
if [ -f $1 ] && [ "$(ps -p `cat $1` | wc -l)" -gt 1 ]; then
echo "stopping server ${ADDRESS}:${2}"
kill -9 `cat $1`
rm $1
else
if [ -f $1 ]; then
echo "server at address ${ADDRESS}:${2} not running"
else
echo "No pid file found for server ${ADDRESS}:${2}"
fi
fi
}
case "$1" in
'start')
start_server $SERVER_PID $SERVER_PROJECTID
;;
'stop')
stop_server $SERVER_PID $SERVER__PROJECTID
;;
'restart')
echo "restarting gunicorn daemon process"
stop_server $SERVER_PID $SERVER_PROJECTID
sleep 2
start_server $SERVER_PID $SERVER_PROJECTID
echo "done restarting"
;;
*)
echo "Usage: $0 { start | stop | restart }"
;;
esac
exit 0
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment