How to 'clear' the port when restarting django runserver

前端 未结 19 2130
盖世英雄少女心
盖世英雄少女心 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:02

    Type fg in the terminal to bring up the background task to the foreground.

    Press Ctrl+C to close/stop the running server.

    0 讨论(0)
  • 2020-12-12 12:03
    fuser -k 8000/tcp
    

    Run in terminal it works in ubutu. 8000 is the port.

    0 讨论(0)
  • 2020-12-12 12:04

    I found this information (originally from Kristinn Örn Sigurðsson) to solve my problem:

    To kill it with -9 you will have to list all running manage.py processes, for instance:

    ps aux | grep -i manage
    

    You'll get an output similar to this if you've started on many ports:

    14770     8264  0.0  1.9 546948 40904 ?        S    Sep19   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8006
    14770    15215  0.0  2.7 536708 56420 ?        S    Sep13   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8001
    14770    30144  0.0  2.1 612488 44912 ?        S    Sep18   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8000
    14770    30282  0.0  1.9 678024 40104 ?        S    Sep18   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8002
    14770    30592  0.0  2.1 678024 45008 ?        S    Sep18   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8003
    14770    30743  0.0  2.1 678024 45044 ?        S    Sep18   0:00 /usr/local/bin/python manage.py runserver 0.0.0.0:8004
    

    Then you'll have to select the pid (which is the second number on the left) for the right manage.py process (python manage.py runserver... etc) and do:

    kill -9 pid
    

    For the above example, if you wanted to free up port 8000, you'd do:

    kill -9 30144
    
    0 讨论(0)
  • 2020-12-12 12:07

    This error is due to the server already running.

    Background

    I am answering on a more general level not specific to Django like the original question asks. So that those that land here from Google can easily fix the problem.

    Solution

    When you need to clear a port, all you need to do is these two steps

    1. In the terminal run fg
    2. Press Control-C (if on a mac)

    Explanation

    fg brings the process to the foreground. Then Control-C stops the server.

    Example

    I was actually having this issue with my port 8000 when running an angular app. I was getting an error when I ran npm start

    Failed at the angular-seed@0.0.0 start script error

    So I ran fg, then I stopped the server with Control-C

    fg

    Then I was able to successfully run the server

    npm start

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-12 12:12

    Like mipadi said, you should be terminating the server (ctrl+c) and returning to the command prompt before calling manage.py runserver again.

    The only thing that could be disrupting this would be if you've somehow managed to make runserver act as a daemon. If this is the case, I'm guessing you're using the Django test server as the actual web server, which you should NOT do. The Django test server is single threaded, slow and fragile, suitable only for local development.

    0 讨论(0)
提交回复
热议问题