Jan 18, 2015

Auto reload while development

While developing web application using Python (or Ruby), I often run web server by ourself. If a bug was introduced, web server would die. After debugged it,  I have to re-run it manually! Well, today I figure out a way to restart it automatically ^^. Basically a Bash script will loop and restart on your behalf.

To having this work for you, replace PYTHON_APP (or entire script function) to your specific needs.
Have fun!

#!/bin/bash
SLEEP_INTERVAL=2 # second
PYTHON_APP="python3 -m tasty serve:web --port=9100"
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
script () {
  cd $DIR
  PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 PYTHONDONTWRITEBYTECODE=1 PYTHONDEBUG=1 PYTHONIOENCODING=utf8 $PYTHON_APP
}
echo "Development mode."
until script; do
    echo "Crashed with exit code $?.  Respawning.." >&2
    sleep $SLEEP_INTERVAL
done

No comments:

Post a Comment

New comment