69

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

Antonius Common
  • 2,611
  • 5
  • 30
  • 32
  • This problem applies to more than just Django, So perhaps we can change the title? I ran into this error when running an Angular app. I found this question with google search `how to kill port 8000` yet this applied to me as well. By changing the title and making the scope more general, we could prevent some redundant questions later. – JGallardo May 21 '14 at 19:04
  • try this `sudo fuser -k 8001/tcp` – hassanzadeh.sd Aug 26 '20 at 13:38

19 Answers19

125

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
SaiyanGirl
  • 14,162
  • 11
  • 38
  • 53
Meilo
  • 3,128
  • 3
  • 25
  • 33
40

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.

mipadi
  • 359,228
  • 81
  • 502
  • 469
  • Ctrl+z is undo. If you don't see a command prompt, your server is still running. – Soviut May 20 '09 at 00:19
  • 15
    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). – mipadi May 20 '09 at 04:43
  • 25
    fg brings it to the foreground. Then press ctrl+c to stop/close it. – irl_irl Nov 11 '10 at 17:34
29

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.

abhiomkar
  • 3,392
  • 6
  • 27
  • 23
  • 2
    On mac use: `sudo lsof -t -i tcp:8000 | xargs kill -9` Hat tip: http://stackoverflow.com/questions/20239232/error-that-port-is-already-in-use – Adam Starrh Jun 17 '16 at 12:50
14

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

  • 2
    I have the same issue in leopard. I close it with Ctrl-C and still the port is blocked. – mamcx Jun 17 '09 at 18:31
13
fuser -k 8000/tcp

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

Ranju R
  • 1,955
  • 18
  • 16
9

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

Community
  • 1
  • 1
JGallardo
  • 9,783
  • 7
  • 72
  • 84
7

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

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

mx0
  • 4,605
  • 10
  • 39
  • 47
thatzprem
  • 4,567
  • 30
  • 38
6

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

Community
  • 1
  • 1
Pat
  • 15,269
  • 13
  • 86
  • 106
5
sudo lsof -t -i tcp:8000 | xargs kill -9

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

Unheilig
  • 15,690
  • 193
  • 65
  • 96
Sunny
  • 61
  • 1
  • 3
3

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

sudo fuser -k 8001/tcp
Pulkit Pahwa
  • 1,192
  • 9
  • 7
2

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

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

Python2: $ killall -9 python

Python3: $ killall -9 python3

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

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

Blairg23
  • 8,642
  • 5
  • 61
  • 61
1
netstat -tulpn |grep 8000|awk '{print $7}'|cut -d/ -f 1|xargs kill
hosein
  • 499
  • 5
  • 23
0

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

mamcx
  • 14,839
  • 22
  • 90
  • 170
0

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

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

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

Mark
  • 15,245
  • 6
  • 95
  • 113
0

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

Nabin
  • 9,681
  • 7
  • 58
  • 91
0

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
Community
  • 1
  • 1
jstaab
  • 2,386
  • 21
  • 34
0

Add the following library in manage.py

import os
import subprocess
import re

Now add the following 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.

mahbubcseju
  • 1,878
  • 1
  • 11
  • 15
0

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.

Soviut
  • 79,529
  • 41
  • 166
  • 227
  • 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 – Jiaaro May 20 '09 at 02:20
  • 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. – Soviut May 20 '09 at 05:01
-1
netstat -ntlp

See my complete answer here. https://stackoverflow.com/a/34824239/5215825

Community
  • 1
  • 1
  • `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. – Blairg23 May 25 '17 at 23:26