How to 'clear' the port when restarting django runserver

前端 未结 19 2147
盖世英雄少女心
盖世英雄少女心 2020-12-12 11:23

Often, when restarting Django runserver, if I use the same port number, I get a \'port is already in use\' message. Subsequently, I need to increment the port number each t

19条回答
  •  一向
    一向 (楼主)
    2020-12-12 12:07

    Repost from https://stackoverflow.com/a/27138521/1467342:

    You can use this script in place of ./manage.py runserver. I put it in scripts/runserver.sh.

    #!/bin/bash
    
    pid=$(ps aux | grep "./manage.py runserver" | grep -v grep | head -1 | xargs | cut -f2 -d" ")
    
    if [[ -n "$pid" ]]; then
        kill $pid
    fi
    
    fuser -k 8000/tcp
    ./manage.py runserver
    

提交回复
热议问题