[ACCEPTED]-How to 'clear' the port when restarting django runserver-django

Accepted answer
Score: 127

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

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

ps aux | grep -i manage

You'll 7 get an output similar to this if you've 6 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 5 select the pid (which is the second number 4 on the left) for the right manage.py process 3 (python manage.py runserver... etc) and 2 do:

kill -9 pid

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

kill -9 30144
Score: 40

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

Score: 30

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

sudo fuser -k 8004/tcp

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

Score: 14

No, he's not an idiot guys. Same thing happens 4 to me. Apparently it's a bug with the python 3 UUID process with continues running long 2 after the django server is shutdown which 1 ties the port up.

Score: 11
fuser -k 8000/tcp

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

Score: 9

This error is due to the server already running.

Background

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

Solution

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

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

Explanation

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

Example

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

Failed at the angular-seed@0.0.0 start script error

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

fg

Then 1 I was able to successfully run the server

npm start

Score: 7

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

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

Score: 6

I use pkill -If 'manage.py' (-I means interactive, -f matches more 2 than just the process name). See How to kill all processes with a given partial name? for more 1 info on pkill.

Score: 5
sudo lsof -t -i tcp:8000 | xargs kill -9

If you want to free 8000 port than just 3 copy command and paste in your cmd it will 2 ask for sudo password. And then you are 1 good to go.

Score: 3

If the port number that you are trying is 1 8001, then use this command

sudo fuser -k 8001/tcp
Score: 2

You do not want to simply increment the 9 port number when restarting a Django server. This 8 will result in having multiple instances 7 of the Django server running simultaneously. A 6 better solution is to kill the current instance 5 and start a new instance.

To do this, you 4 have multiple options. The easiest is

Python2: $ killall -9 python

Python3: $ killall -9 python3

If 3 for some reason, this doesn't work, you 2 can do

$ kill <pid> where <pid> is the process id found from 1 a simple $ ps aux | grep python command.

Score: 1
netstat -tulpn |grep 8000|awk '{print $7}'|cut -d/ -f 1|xargs kill

0

Score: 1

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

You can use this script in 1 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
Score: 0

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

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

Score: 0

In Leopard, I bring on the Activity Monitor 1 and kill python. Solved.

Score: 0

Happened so often that I wrote an alias 5 to kill the process with python in the name (careful 4 if you have other such processes). Now I 3 just run (no Ubuntu)

kill $(ps | grep "python" | awk "{print $1}")

You can even add python manage.py runserver ... to 2 the same alias so you can restart with two 1 keystrokes.

Score: 0

You must have been doing control + z .. Instead 2 do control + c that will kill the server 1 session... Cheers!!!

Score: 0

Add the following library in manage.py

import os
import 2 subprocess
import re

Now add the following 1 python code after if __name__ == "__main__":

ports = ['8000']
popen = subprocess.Popen(['netstat', '-lpn'],
                         shell=False,
                         stdout=subprocess.PIPE)
(data, err) = popen.communicate()
pattern = "^tcp.*((?:{0})).* (?P<pid>[0-9]*)/.*$"
pattern = pattern.format(')|(?:'.join(ports))
prog = re.compile(pattern)
for line in data.split('\n'):
    match = re.match(prog, line)
    if match:
        pid = match.group('pid')
        subprocess.Popen(['kill', '-9', pid])

This will first find the process id of port 8000 , will kill it and then restart your project. Now each time you don't need to kill the pid manually.

More Related questions