Last active
August 14, 2019 06:50
-
-
Save mattseymour/5174229f3d0281f35f1e to your computer and use it in GitHub Desktop.
Debian gunicorn init.d script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
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