Skip to content

Instantly share code, notes, and snippets.

@chalmerj
Created December 18, 2011 02:26
Show Gist options
  • Save chalmerj/1492154 to your computer and use it in GitHub Desktop.
Save chalmerj/1492154 to your computer and use it in GitHub Desktop.
Init Script for Graphite/Gunicorn
#! /bin/sh
### BEGIN INIT INFO
# Provides: gunicorn-graphite
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $nginx
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: gunicorn + nginx ubuntu init script
# Description: gunicorn + nginx ubuntu init script
### END INIT INFO
# Author: Jeremy Chalmer
# Original Author: Nicolas Kuttler <hidden>
# http://drumcoder.co.uk/blog/2011/aug/26/installing-graphite-debian/
#
# This init script was written for Ubuntu 11.10 using start-stop-daemon.
#
# Note: This init script does *NOT* use virtualenv.
#
# Enable with update-rc.d gunicorn-graphite
# Source init-functions:
#source /lib/lsb/init-functions
. /lib/lsb/init-functions
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# ----- gunicorn_django configuration -----
export PYTHONPATH=/opt/graphite/webapp
PROJECT=/opt/graphite/webapp/graphite
PORT=8000
LOGDIR=/var/log/gunicorn # Make sure this directory is writeable by USER!
IP=localhost
WORKERS=1
USER=www-data
GROUP=www-data
# ----- end gunicorn_django configuration -----
# Name of executable daemon
NAME=gunicorn_django
DESC=$NAME
LOGFILE="$LOGDIR/$NAME.log"
# Path to Executable
DAEMON=$(which $NAME)
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Lay out the arguments for gunicorn using the configuration from above.
GUNICORN_ARGS=" --user=$USER \
--group=$GROUP \
--daemon \
--workers=$WORKERS \
--bind=$IP:$PORT \
--pid=$PIDFILE \
--name=$NAME \
--log-file=$LOGFILE \
--log-level=info \
-D settings.py"
# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then {
echo "Couldn't find $DAEMON or not executable"
exit 99
}
fi
# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
# Test to see if the daemon is already running - return 1 if it is.
start-stop-daemon --start --pidfile $PIDFILE --make-pidfile --chdir $PROJECT \
--exec $DAEMON --background --test -- $GUNICORN_ARGS > /dev/null || return 1
# Start the daemon for real, return 2 if failed
start-stop-daemon --start --pidfile $PIDFILE --make-pidfile --chdir $PROJECT \
--exec $DAEMON -- $GUNICORN_ARGS || return 2
}
#
# Function that relaods the daemon's settings by sending SIGHUP
#
do_reload()
{
start-stop-daemon --stop --quiet --pidfile $PIDDILE --exec $DAEMON --signal 1
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile $PIDFILE --exec $DAEMON
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
if [ -e "$PIDFILE" ]; then {
rm -f $PIDFILE
}
fi
return "$RETVAL"
}
# Display / Parse Init Options
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_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 "Stopping $DESC" "$NAME"
do_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"
do_stop
case "$?" in
0|1)
do_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"
do_reload
case "$?" in
0) log_end_msg 0 ;;
*) log_end_msg 1 ;;
esac
;;
status)
if [ -s $PIDFILE ]; then
pid=`cat $PIDFILE`
kill -0 $pid >/dev/null 2>&1
if [ "$?" = "0" ]; then
echo "$NAME is running: pid $pid."
RETVAL=0
else
echo "Couldn't find pid $pid for $NAME."
RETVAL=1
fi
else
echo "$NAME is stopped (no pid file)."
RETVAL=1
fi
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment