Skip to content

Instantly share code, notes, and snippets.

@dmukhg
Last active December 11, 2015 01:48
Show Gist options
  • Save dmukhg/4525738 to your computer and use it in GitHub Desktop.
Save dmukhg/4525738 to your computer and use it in GitHub Desktop.
Debian style init script for a django web-application running on a gunicorn server via its own virtualenv.
#! /bin/sh
### BEGIN INIT INFO
# Provides: planner
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the planner web app on a gunicorn server
# Description: starts the planner web app using start-stop-daemon
### END INIT INFO
# www-data is the default server user on debian
USER=www-data
NAME="planner"
DESC="Daily Planner Application"
# The command used to run gunicorn. This one is tailored for quick use with
# django apps
APPDIR="/home/schatten/code/schatten/planner"
VENV_CMD="/bin/activate"
PIDFILE="/tmp/gunicorn_"$NAME".pid"
GUNICORN_CMD="/bin/gunicorn_django"
GUNICORN_CONFIG="/gunicorn_config.py"
GUNICORN_OPTIONS="-p $PIDFILE --daemon -c $APPDIR$GUNICORN_CONFIG"
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
. /lib/lsb/init-functions
start()
{
# Return
# 0 if daemon succesfully started
# 1 if daemon already running
# 2 if daemon couldn't be started.
if [ -e $PIDFILE ]; then
return 1
fi
cd $APPDIR
. $APPDI$VEND_CMD
$APPDIR$GUNICORN_CMD $GUNICORN_OPTIONS
if [ $? = 0 ]; then
return 0
else
return 2
fi
}
stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
rm $PIDFILE
kill -15 $PID
if [ $? = 0 ]; then
return 0
else
return 2
fi
else
return 1
fi
}
reload()
{
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
kill -HUP $PID
return $?
fi
return 2
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0;;
2) [ "$VERBOSE" != no ] && log_end_msg 1;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0;;
2) [ "$VERBOSE" != no ] && log_end_msg 1;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
stop
case "$?" in
0|1)
start
case "$?" in
0) log_end_msg 0;;
1) log_end_msg 1;; # Old process is still running
*) log_end_msg 1;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
reload)
log_daemon_msg "Reloading $DESC" "$NAME"
reload
case "$?" in
0) log_end_msg 0;;
*) log_end_msg 1;;
esac
;;
force-reload)
stop && start
;;
*)
echo "Usage: $0 {start|stop|restart|reload|force-reload}"
exit 3
esac
exit 0
@dmukhg
Copy link
Author

dmukhg commented Jan 13, 2013

Usage:
sudo /etc/init.d/planner start|stop|restart|reload|force-reload

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