Skip to content

Instantly share code, notes, and snippets.

@dpetzold
Created September 7, 2010 22:28
Show Gist options
  • Save dpetzold/569265 to your computer and use it in GitHub Desktop.
Save dpetzold/569265 to your computer and use it in GitHub Desktop.
Init script for fastcgi and django.
#! /bin/sh
### BEGIN INIT INFO
# Provides: FastCGI servers for Django
# Required-Start: networking
# Required-Stop: networking
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Start FastCGI servers with Django.
# Description: Django, in order to operate with FastCGI, must be started
# in a very specific way with manage.py. This must be done
# for each DJango web server that has to run.
### END INIT INFO
#
# Author: Derrick Petzold
# <contact@derrickpetzold.com>
# Author: Guillermo Fernandez Castellanos
# <guillermo.fernandez.castellanos AT gmail.com>,
#
# http://gist.github.com/569265
#
#### SERVER SPECIFIC CONFIGURATION
SITES_PATH=/var/sites
RUNFILES_PATH=/tmp
HOST=127.0.0.1
RUN_AS=www-data
FCGI_METHOD=prefork
EXTRA_OPTIONS="maxchildern=1 maxrequests=1"
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="FastCGI servers"
NAME=$0
SCRIPTNAME=/etc/init.d/$NAME
#
#
# Function that starts the daemon/service.
#
d_start_all()
{
for SITE in `ls --color=never $SITES_PATH`
do
if [ -e "$SITES_PATH/$SITE/$SITE/manage.py" ]; then
d_start $SITE
fi
done
}
d_start()
{
# Starting all Django FastCGI processes
echo -n "$SITE, "
if [ -f $RUNFILES_PATH/$SITE.pid ]; then
echo -n " already running"
else
start-stop-daemon --start --quiet \
--pidfile $RUNFILES_PATH/$SITE.pid \
--chuid $RUN_AS --exec /usr/bin/env -- python \
$SITES_PATH/$SITE/$SITE/manage.py runfcgi \
method=$FCGI_METHOD \
socket=$RUNFILES_PATH/$SITE.socket \
pidfile=$RUNFILES_PATH/$SITE.pid \
$EXTRA_OPTIONS
chmod 400 $RUNFILES_PATH/$SITE.pid
fi
}
#
# Function that stops the daemon/service.
#
d_stop_all() {
# Killing all Django FastCGI processes running
for SITE in `ls --color=never $SITES_PATH`
do
if [ -e "$SITES_PATH/$SITE/$SITE/manage.py" ]; then
d_stop $SITE
fi
done
}
d_stop()
{
SITE=$1
echo -n "$SITE, "
start-stop-daemon --stop --quiet --pidfile $RUNFILES_PATH/$SITE.pid \
|| echo -n " not running"
if [ -f $RUNFILES_PATH/$SITE.pid ]; then
rm $RUNFILES_PATH/$SITE.pid
fi
}
ACTION="$1"
SITE="$2"
case "$ACTION" in
start)
echo -n "$NAME starting $DESC: "
if [ $SITE ]; then
d_start $SITE
else
d_start_all
fi
echo "."
;;
stop)
echo -n "$NAME stopping $DESC: "
if [ $SITE ]; then
d_stop $SITE
else
d_stop_all
fi
echo "."
;;
restart|force-reload)
echo -n "$NAME restarting $DESC: "
if [ $SITE ]; then
d_stop $SITE
sleep 1
d_start $SITE
else
d_stop_all
sleep 1
d_start_all
fi
echo "."
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload} {sitename}" >&2
exit 3
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment