Prevent django runserver from crashing or reload on save fix

django

Solution

I use a simple bash script for this. Here's a one-liner you can use:

$ while true; do python manage.py runserver; sleep 2; done

That will wait 2 seconds before attempting to restart the server. Insert whatever you think is a sane value.

I usually write this as a shell script named `runserver.sh`, put it in my project root (the same directory with manage.py in it) and add it to the gitignore.

while true; do
  echo "Re-starting Django runserver"
  python manage.py runserver
  sleep 2
done

If you do this, remember to `chmod +x runserver.sh`, then you can execute it with:

./runserver.sh

Use `Ctrl-c Ctrl-c` to exit.

Problem

When I run `./manage.py runserver`, it often crashes because of mistakes I make, e.g. copy/paste code and save before modifying it or having a syntax error. Then I have to rerun the server. Is it possible to somehow have the server still running and then reload on my subsequent save so I don't have to run the server manually again?

Original source