How to 'clear' the port when restarting django runserver

74,325

Solution 1

You're getting that message because the server is already running (possibly in the background). Make sure to kill the process (bring it to the foreground and press ctrl-c) to stop the process.

Solution 2

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

Solution 3

If the ps aux command (as per Meilo's answer) doesn't list the process that you wanted to kill but shows the port active in netstat -np | grep 8004 network activity, try this command (worked on Ubuntu).

sudo fuser -k 8004/tcp

where as, 8004 is the port number that you want to close. This should kill all the processes associated with port 8004.

Solution 4

fuser -k 8000/tcp

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

Solution 5

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

Share:
74,325
Antonius Common
Author by

Antonius Common

Updated on July 05, 2022

Comments

  • Antonius Common
    Antonius Common over 1 year

    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 time to avoid this.

    It's not the case on all servers, however, so I'm wondering how I might achieve this on the current system that I'm working on?

    BTW, the platform is Ubuntu 8.10

  • Sami Ahmed Siddiqui
    Sami Ahmed Siddiqui almost 15 years
    Ctrl+z is undo. If you don't see a command prompt, your server is still running.
  • Jiaaro
    Jiaaro almost 15 years
    no, I've had this problem too... specifially when allowing eclipse to manage terminating the test server... I think it might happen as a result of something in the test server freezing up
  • mipadi
    mipadi almost 15 years
    ctrl-z puts a process in the background, so one could see a command prompt, even though the server is still running (in the background).
  • Sami Ahmed Siddiqui
    Sami Ahmed Siddiqui almost 15 years
    I'd recommend simply starting the test server and letting it run. It automatically detects changes to your python files and restarts itself. No need to hit a 'run' button.
  • irl_irl
    irl_irl over 13 years
    fg brings it to the foreground. Then press ctrl+c to stop/close it.
  • spiffyman
    spiffyman over 12 years
    Just want to add that this solution worked for a colleague running Python 2.6.2 on OS X Lion, Django 1.2.0b1. Thanks!
  • Alejandro García Iglesias
    Alejandro García Iglesias over 10 years
    I stopped the Django server with Ctrl+C but the process kept running. This answer was the solution, it should be accepted.
  • Jeff Sheffield
    Jeff Sheffield over 9 years
    +1 Nice answer: I was about to go write this response, because netstat gets to the heart of the matter.
  • Vasiliy Toporov
    Vasiliy Toporov almost 9 years
    "ps aux | grep -i runserver" seemed to give less output than "ps aux | grep -i manage".
  • Adam Starrh
    Adam Starrh almost 8 years
    On mac use: sudo lsof -t -i tcp:8000 | xargs kill -9 Hat tip: stackoverflow.com/questions/20239232/…
  • Blairg23
    Blairg23 almost 7 years
    netstat -ntlp just shows the active connections, ports, and PIDs. In fact, a better way to get the PID would be ps aux or more specifically ps aux | grep python to see the python and python3 instances.
  • wanderer0810
    wanderer0810 over 6 years
    I'm getting this error, and have tried this. Do you have any other solutions?
  • wanderer0810
    wanderer0810 over 6 years
    It turns out that postgres was not running. If Postgres is not running, it doesn't listen on port 5432. I started it and it worked like a charm.