Skip to content

Instantly share code, notes, and snippets.

@aidos
Created August 4, 2010 21:29
Show Gist options
  • Save aidos/508838 to your computer and use it in GitHub Desktop.
Save aidos/508838 to your computer and use it in GitHub Desktop.
#!/bin/sh
# start with
# cd /path/to/your/django/project
# sudo keep_alive.sh start projectname portnumber
# in another window
# sudo keep_alive.sh restart/stop projectname
# or kick the system with:
# touch /path/to/keep_alive/keep_alive_cache/projectname.restart
# in Textmate I automatically run this on saving each pythhon file
# and if it all goes wrong:
# sudo kill `ps -e | grep keep | grep -v "grep keep" | awk '{print $1}'`
# you can also uncomment the stuff in the watcher if you want it to restart when python files change
COMMAND=$1
MYAPP=$2
PORT=$3
SITE_HOME="`pwd`"
cd "`dirname $0`"
SCRIPT_HOME="`pwd`"
CACHE="$SCRIPT_HOME/keep_alive_cache"
PIDFILE="$CACHE/${MYAPP}.pid"
RUNFILE="$CACHE/${MYAPP}.run"
RESTARTFILE="$CACHE/${MYAPP}.restart"
function failure () {
STATUS=$?;
echo; echo "Failed $1 (exit code ${STATUS}).";
exit ${STATUS};
}
function start_server () {
python "$SITE_HOME/manage.py" runfcgi host=127.0.0.1 method=threaded port=$PORT pidfile=$PIDFILE daemonize=false
}
function restart_server () {
kill `cat $PIDFILE` || failure "restarting fcgi"
rm $PIDFILE
}
function stop_server () {
# stop django
rm $RUNFILE
restart_server
}
case "$COMMAND" in
start)
# make sure we have somewhere to store our files
if [ ! -d $CACHE ] ; then
mkdir $CACHE
fi
echo "Starting watcher"
./keep_alive.sh watch $MYAPP &
watcher_pid=$!
echo "Starting fcgi"
touch $RUNFILE
while [ -e $RUNFILE ]; do
echo "Restarting fcgi: "
start_server
done
echo "fcgi stopped"
kill $watcher_pid
echo "watcher stopped"
;;
stop)
printf "Stopping fcgi: "
stop_server
echo "Done."
;;
restart)
echo "Restarting fcgi: "
restart_server
echo "Done."
;;
watch)
echo "Watching code: "
while true; do
if [ -e $RESTARTFILE ] ; then
rm $RESTARTFILE
./keep_alive.sh restart $MYAPP
# else
# found="`find $SITE_HOME -iname "*.py" -mtime -2s`"
# if [ -n "$found" ] ; then
# echo "changed : $found"
# ./keep_alive.sh restart $MYAPP
# fi
fi
sleep 1;
done
;;
*)
echo "Usage: $0 {start|stop|restart|watch}"
;;
esac
exit 0
@aidos
Copy link
Author

aidos commented Aug 4, 2010

Simple bash script to allow you to run django in fastcgi mode on development. It always keeps a copy of django running in the foreground so that you can drop into the debugger during runtime.

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