DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Steven has posted 1 posts at DZone. View Full User Profile

Simple Init.d Script For Django Using Fastcgi

07.15.2008
| 6161 views |
  • submit to reddit
        
#!/bin/sh
#
# chkconfig:	345 85 15
# description:	FastCGI server for Django


# Source function library
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

PROJ=myproject					# Django project name
PROJSPATH=/home/user/www			# The folder Django project is in
SOCKSPATH=/tmp					# Where you put UNIX socket file
PIDSPATH=/var/run				# Where you put .pid file
RUNAS=root					# Set a different user to run the FastCGI server

SRVNAME=fcgi-$PROJ				# Process name

# Maximum requests for a child to service before expiring
#MAXREQ=
# Spawning method - prefork or threaded
#METHOD=
# Maximum number of children to have idle
#MAXSPARE=
# Minimum number of children to have idle
#MINSPARE=
# Maximum number of children to spawn
#MAXCHILDREN=

start () {
	# Check if the service is already running?
	if [ ! -f $PIDSPATH/$SRVNAME.pid ]; then
		echo -n $"Starting $SRVNAME..."
		daemon --user $RUNAS $PROJSPATH/$PROJ/manage.py runfcgi pidfile=$PIDSPATH/$SRVNAME.pid \
			socket=$SOCKSPATH/$SRVNAME.sock \
			${MAXREQ:+maxrequests=$MAXREQ} \
			${METHOD:+method=$METHOD} \
			${MAXSPARE:+maxspare=$MAXSPARE} \
			${MINSPARE:+minspare=$MINSPARE} \
			${MAXCHILDREN:+maxchildren=$MAXCHILDREN} \
			${DAEMONISE:+damonize=True}
		echo
		chmod 777 $SOCKSPATH/$SRVNAME.sock
		RETVAL=$?
	else
		echo $"$SRVNAME is already running."
	fi
}

stop() {
	# Stop daemons.
	if [ -f $PIDSPATH/$SRVNAME.pid ]; then
		echo -n $"Stopping $SRVNAME..."
		killproc -p "$PIDSPATH/$SRVNAME.pid" -d 60 $SRVNAME
		echo
		# Delete pidfile only when Django was called successfully
		if [ $? -eq 0 ]; then
			rm -f $PIDSPATH/$SRVNAME.pid "$SRVNAME.pid"  >/dev/null 2>&1
		fi
	else
		echo $"$SRVNAME is NOT running."
	fi

}

RETVAL=0

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status -p "$PIDSPATH/$SRVNAME.pid" $SRVNAME
		RETVAL=$?
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 3
		;;
esac

exit $RETVAL