Ошибка port already in use

Restarting the Django server displays the following error:

this port is already running....

This problem occurs specifically on Ubuntu and not other operating systems. How can I free up the port to restart the server?

Jeff Bauer's user avatar

Jeff Bauer

13.8k9 gold badges51 silver badges73 bronze badges

asked Nov 27, 2013 at 10:00

Ashish  Kumar Saxena's user avatar

3

A more simple solution just type sudo fuser -k 8000/tcp.
This should kill all the processes associated with port 8000.

EDIT:

For osx users you can use sudo lsof -t -i tcp:8000 | xargs kill -9

answered Nov 27, 2013 at 10:53

Mounir's user avatar

8

netstat -ntlp

It will show something like this.

   Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State           PID/Program name    
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      6599/python         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -                   
tcp        0      0 192.168.124.1:53        0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN     

So now just close the port in which Django/python running already by killing the process associated with it.

kill -9 PID

in my case

kill -9 6599

Now run your Django app.

answered Jan 16, 2016 at 6:29

Shekhar Singh Choudhary's user avatar

2

ps aux | grep -i manage

after that you will see all process 


ubuntu@ip-10-154-22-113:~/django-apps/projectname$ ps aux | grep -i manage
ubuntu    3439  0.0  2.3  40228 14064 pts/0    T    06:47   0:00 python manage.py runserver project name
ubuntu    3440  1.4  9.7 200996 59324 pts/0    Tl   06:47   2:52 /usr/bin/python manage.py runserver project name
ubuntu    4581  0.0  0.1   7988   892 pts/0    S+   10:02   0:00 grep --color=auto -i manage


kill -9 process id


e.d kill -9 3440


`enter code here`after that :

python manage.py runserver project name

answered Nov 27, 2013 at 10:03

Ashish  Kumar Saxena's user avatar

3

By default, the runserver command starts the development server on the internal IP at port 8000.

If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

python manage.py runserver 8080

Stephen Rauch's user avatar

Stephen Rauch

47.5k31 gold badges106 silver badges135 bronze badges

answered Oct 31, 2018 at 1:15

Freddy's user avatar

FreddyFreddy

1441 silver badge5 bronze badges

2

lsof -t -i tcp:8000 | xargs kill -9

Samsul Islam's user avatar

Samsul Islam

2,5682 gold badges17 silver badges23 bronze badges

answered Feb 4, 2020 at 6:14

Nandy's user avatar

NandyNandy

3313 silver badges5 bronze badges

1

Sorry for comment in an old post but It may help people

Just type this on your terminal

killall -9 python3

It will kill all python3 running on your machine and it will free your all port. Greatly help me when to work in Django project.

answered Feb 23, 2020 at 12:28

Antu's user avatar

AntuAntu

2,1473 gold badges25 silver badges40 bronze badges

3

>> ps aux | grep manage

 ubuntu    3438  127.0.0  2.3  40256 14064 pts/0    T    06:47   0:00 python manage.py runserver

>> kill -9 3438

Antu's user avatar

Antu

2,1473 gold badges25 silver badges40 bronze badges

answered Sep 4, 2017 at 17:40

Basant Rules's user avatar

1

We don’t use this command { sudo lsof -t -i tcp:8000 | xargs kill -9 } Because it’s close all tabs…You should use to

ps -ef | grep python

kill -9 process_id

ps -ef | grep python (show all process with id)

kill -9 11633
(11633 is a process id to :- /bin/python manage.py runserver)

answered Jul 10, 2017 at 11:33

Mr Singh's user avatar

Mr SinghMr Singh

3,8864 gold badges40 silver badges60 bronze badges

Type ‘fg’ as command after that Ctrl-C.
Command:
Fg will show which is running on background. After that Ctrl-C will stop it.

fg
ctl-c

Samsul Islam's user avatar

Samsul Islam

2,5682 gold badges17 silver badges23 bronze badges

answered Nov 26, 2019 at 0:15

mahbubcseju's user avatar

mahbubcsejumahbubcseju

2,1802 gold badges15 silver badges21 bronze badges

2

This is an expansion on Mounir’s answer. I’ve added a bash script that covers this for you. Just run ./scripts/runserver.sh instead of ./manage.py runserver and it’ll work exactly the same way.

#!/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

answered Nov 25, 2014 at 23:11

jstaab's user avatar

jstaabjstaab

3,34926 silver badges40 bronze badges

Click the arrow in the screenshot and find the bash with already running Django server. You were getting the message because your server was already running and you tried to start the server again.

enter image description here

answered Aug 5, 2020 at 19:38

Python Newbie's user avatar

  1. In terminal, Type ps aux | grep runserver
  2. Hit Enter
  3. Use PID among the result execute kill -9 <PID>

For an instance, If result of step 1 is as follow

root      1041  0.0  0.1 266912 34580 pts/3    S+   11:31   0:01 python3 manage.py runserver 0.0.0.0:3030
root      1696  4.5  0.1 126128 40708 ?        S    Feb14 925:43 /usr/local/bin/python manage.py runserver 0.0.0.0:8000

1041 and 1696 are PIDs. We need to choose whichever process we want to kill among them.

answered Feb 28, 2022 at 9:03

Rahul Rahatal's user avatar

For me, this happens because my API request in Postman is being intercepted by a debugger breakpoint in my app… leaving the request hanging. If I cancel the request in Postman before killing my app’s server, the error does not happen in the first place.

—> So try cancelling any open requests you are making in other programs.

On macOS, I have been using sudo lsof -t -i tcp:8000 | xargs kill -9 when I forget to cancel the open http request in order to solve error = That port is already in use. This also, complete closes my Postman app, which is why my first solution is better.

answered May 15, 2019 at 14:30

Kermit's user avatar

KermitKermit

4,7924 gold badges40 silver badges74 bronze badges

Dont use CTRL + Z to stop server, use CTRL + C to stop the server, I had also had the same problem in my linux (fedora) , I used to stop the server using CTRL + Z and again I used to kill the server using sudo fuser -k 8000/tcp command, which worked fine. But later when I started using CTRL + C , I didnot get that port running issue anymore.

answered Jan 17, 2022 at 5:24

Bickky Sahani's user avatar

if you have face this problem in mac you just need to open activity monitor and force quite python then try again

enter image description here

answered Feb 1, 2020 at 19:43

Ahmed Safadi's user avatar

Ahmed SafadiAhmed Safadi

4,36236 silver badges33 bronze badges

In case You are using the VSC’s screen terminal, The error might be due to the fact that you already runserver in some other shell.

Just click on the dropbox on the left of the + sign in the header of the terminal of VSC and select some other shell and check if the server is already running there. Quit that server and you are ready to launch a another server.

answered May 14, 2020 at 7:57

Yash Verma's user avatar

Yash VermaYash Verma

4215 silver badges4 bronze badges

0

I was trying all the solutions but they were not working i suggest you to keep press the power button or if your battery is removeable then remove it all the process will be killed and your local host will be reset

answered Oct 2, 2022 at 10:27

hassan 's user avatar

hassan hassan

12 bronze badges

It must be frustrating when you run the Django local server, and you get the “Error: That port is already in use.” You cannot serve your new Django project on a local development setup.

The main cause of the error is serving more than one Django project using the same default port, 8000.

Fortunately, solving the error is a very straightforward process.

To solve the  “error: that port is already in use” in Django, you need to stop the currently running server on port 8000 and free the port for the new Django project.

Here are three ways to solve “Error: That port is already in use” in Django:

1. Locate the Terminal that you have other Django runserver running and press CTRL + C to quit the server.

That way, you allow another Django project to use the port to serve your new Django project.

2. Specify the port you want to serve your Django project when running the local server, Django runserver.

To do that, run python manage.py runserver 0.0.0.0:9000. The new Django project should be served on port 9000, and the old Django project should be served using the default port, 8000. You may use any port number you may want. Using this method, you can run more than two Django projects on the same local machine.

3. By opening the Terminal and typing htop, press function key F3, type ‘runserver’ and press function key F9.

The process kills the Django server running in the background. If you want to stop the old Django runserver process, but you do not have access to the Terminal that it is running, you may use htop to kill the runserver background process. 

Let’s see how we can implement the three methods to solve “Error: That port is already in use” in detail.

Method 1: How to quit Django local development server

Locate the port that you have your Django local server running. 

The Terminal window should look like this:

How to run Django local server on Linux

Press CTRL + C to quit the Django local development server.

It is as simple as that – nothing else that you need to do.

Open another Terminal for the new Django project and try running python manage.py runserver.

Django’s local development server should run successfully because you only have one Django project on port 8000.

There should be no more than one runserver instances running concurrently. Thus, the “Error: That port is already in use” should not show again.

Method 2: How to serve a Django project on a different port

Another way to solve “Error: That port is already in use” is to serve your new Django project on another port different from the default port 8000.

To do that, open a new Terminal window and activate the virtual environment you have installed the new Django project.

Run python manage.py runserver 0.0.0.0:9000 and the error should not appear.

The new Django project should be served using port 9000.

You may use any other port to serve your new Django project.

However, do not serve your Django project on ports already used by other services such as databases. For example, if you use port 5432, which is used by PostgreSQL databases, you will still get the “Error: That port is already in use.”

Serving a new Django project on a different port has its advantage because you can serve different Django projects on the same local machine. Provided you do not have two projects sharing the same port – that is impossible.

As you can see in the screenshot below, I am serving two Django projects, the Django project in the left pane on default port 8000 and the right pane running another Django project on port 9000.

How to serve two Django projects on a local development server (Linux)

When you access each Django project on the browser, you must specify the specific port number used to serve the Django project.

So, if you are serving a Django project on port 9000, you should open 127.0.0.1:9000 in your browser.

Similarly, if you have another project on port 4000, you should type 127.0.0.1:4000 in the address bar.

The screenshot below shows how to access two Django projects running on ports 8000 and 4000 in the same local machine on a browser.

How to serve two Django websites on different ports

Method 3: How to kill Django runserver process running in the background

To kill the background Django runserver process, you need to use htop to browse the process in the background and send a signal to kill it. That way, no Django server instances are running on default port 8000.

The final method involves killing the runserver process running in the background. The reasons that could have led to the Django runserver running in the background would be:

  1. Pressing CTRL + Z, which suspends the server.
  2. Closing the Terminal running the Django server.

By suspending or closing the Terminal window with the Django project, you can only quit the Django development server using htop. htop is a utility you can use to manage background processes running on your Linux machine.

Let’s see how you use htop to quit the Django server running in the background.

But first, you must install the utility before using it. 

Here’s how to install htop on your Linux machine:

Open a new Terminal window and type the following:

sudo apt update 

sudo apt install htop

After a successful install, htop should be ready to use.

Using the same Terminal window or a new one, type htop and press Enter.

How to run htop on Linux

Press function key F3 to search the Django server running in the background.

Type ‘runserver’ and you should see the active Django background process highlighted in a bright color.

How to search a background process using htop on Linux

Press function key F9 to kill the Django development server process.

Use the up-down keys and use signal 9 SIGKILL to quit the Django development server.

Press Enter to kill the process.

How to kill a Django background process using htop on Linux

Managing Django “Error: That port is already in use is as easy as that.

You may choose to serve only one Django project using the default server.

Alternatively, you may choose to server more than one Django project on different port numbers. 

When serving Django projects on different ports, you should not have two Django projects sharing the same port.

Besides, avoid using port numbers used by other services such as databases.

If you have a PostgreSQL database, then port 5432 should not be used to serve Django websites on a local machine.

And that’s it basically for this article.

See you next time, Dev!

  • nodemon -v: 1.14.12
  • node -v: 9.4.0
  • Operating system/terminal environment: macOS High Sierra / zsh
  • Command you ran:
$ concurrently -k "webpack -d --config ./webpack/config/webpack.dev.config.js --watch" "nodemon ./bin/app.bundle"

This was my first attempt to update the Webpack build and restart the server simultaneously using concurrently. I thought maybe concurrently was causing some issues, so I opted for the Nodemon Webpack Plugin instead, only to find I was getting the same issue.

Expected behaviour

Nodemon should kill the server clean every time it restarts, or when it exits.

Actual behaviour

I’m informed that my server’s port is already running when Nodemon starts it («Port 8080 is already in use»), even after forcibly killing the server on that port using the process ID listened with lsof -i. When files change and Nodemon restarts, sometimes it actually listens on the port, but most of the time it just complains the port is already in use. A clean reboot of my system usually makes the problem go away temporarily.

Steps to reproduce

Unfortunately, an extremely tricky issue to reproduce, seeing there does not seem to be a clear case of when it happens or when it doesn’t. It happens randomly, very often but not frequently. Here is how my server is being set up, though:

const port = normalizePort(process.env.PORT || '8080');
app.set('port', port);

if (port) {
  const server = http.createServer(app);

  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);

  process.on('uncaughtException', () => server.close());
  process.on('SIGTERM', () => server.close());
  process.on('exit', () => server.close());

  function onError(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }

    const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port.toString()}`;

    switch (error.code) {
      case 'EACCES':
        console.error(`${bind} requires elevated privileges`);
        process.exit(1);
        break;
      case 'EADDRINUSE':
        console.error(`${bind} is already in use`);
        process.exit(1);
        break;
      default:
        throw error;
    }
  }

  function onListening() {
    const addr = server.address();
    const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
    debug(`Listening on ${bind}`);
    console.log(`Server started on ${bind}`);
  }
}

function normalizePort(val) {
  const port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

If applicable, please append the --dump flag on your command and include the output here ensuring to remove any sensitive/personal details or tokens.

First published on MSDN on Oct 05, 2016
It’s 3 AM on a Saturday morning and you receive that dreaded phone call – «The SQL Server just had maintenance. We rebooted and now SQL won’t start»! You dig into the error logs and see something like this:

2016-07-12 22:41:20.58 spidxxs Server TCP provider failed to listen on [ ‘any’ <ipv6> 1433]. Tcp port is already in use.
2016-07-12 22:41:20.58 spidxxs Error: 17182, Severity: 16, State: 1.

You know there were no services using your port before the maintenance, so what’s going on here and how do you find out who is using SQL’s port?

There are a two situations that will cause this error message. First, the most obvious situation, is another Service or Application running on the server that uses the same TCP port as the SQL Server. In the case above, that would be tcp/1433. You can quickly check which process is using the port by running

  1. tcpview.exe from SysInternals
  2. PowerShell scripts
  3. Netstat

Examples of each method are listed below. To repro this scenario, I stopped SQL Server and ran a PowerShell script through PowerShell_ISE that listens on tcp/1433.

PowerShell

cls
$tcpPort = "1433"
$tcpConn = Get-NetTCPConnection | Where-Object {$_.LocalPort -eq $tcpPort -or $_.RemotePort -eq $tcpPort}
$tcpConn
$process = $tcpConn | Select-Object OwningProcess -Unique
if ($process -ne $null)
{
    Get-Process | Where-Object {$_.id -eq $process.OwningProcess} | Select-Object Id, ProcessName
}
else
{
    write-output "No services found using that port"
}

NetStat

netstat -p tcp -o -a -n

TCPView

The good news is that this situation is pretty simple to address – identify the owner of this application and then prevent it from running (kill it/stop the service/etc.) or change the port it listens on. There are other ways to handle this situation but none I would recommend at 3 AM on a Saturday.

The second situation involves a client running on the server using the same dynamic port as SQL Server. Every TCP session requires two endpoints – a client endpoint and a server endpoint. Each endpoint is an IP Address/Port combination — also known as a TCP socket. Normally, the client socket code will ask the OS to provide a port from a range of dynamic ports. The server socket is the IP/TCP port of the service the client is connecting to (SQL Server for example). To illustrate this, see the image below. The client port for ssms.exe is 11559 and 11560 (two active TCP sessions). The Remote Port or the SQL Server port is 1433.

So, how do you gain visibility into the dynamic port range that the OS uses? Simply run netsh int ipv4 show dynamicportrange tcp at a Windows command prompt.

Here is the output from my Windows 10 machine

What this means is that a client that requests a client port from the OS will get a free port in the range 1025 through 65534. As you see 1433 is in that range, so if you happen to have a client (think Windows OS system services) that start BEFORE SQL Server does, it could use TCP/1433 for its client socket. I’ve personally had this happen on several occasions. The biggest challenge here is that if it involves a System service, you cannot just stop the service, start SQL and continue on your merry way. You have to reboot and hope the situation does not happen post reboot. I’ve also had this happen through three reboots of the same machine L.

So, how do you fix and prevent a situation like this? There’s another feature within the OS that allows you to tell the OS the TCP ports in the Dynamic Port Range that should be EXCLUDED. Once again, hit a Windows command prompt and execute netsh int ipv4 show excludedportrange tcp . Here is the output from my Windows 10 machine

You see there are already some port ranges that are excluded. These are system services that have exclusions by default – for example, 47001 is WinRM. All that is needed is to run the following with an Administrative Command Prompt netsh int ipv4 add excludedportrange tcp startport=1433 numberofports=1 store=persistent. After running this command, you will see the following excluded ports

After that setting is made, the OS will not give any clients that port. Services can listen on it still, but you prevent clients from getting it when asking the OS for a dynamic port. I would highly recommend you incorporate this into your server load process to safeguard against this issue.

I hope this helps.

In this post, We will try to understand the Web server failed to start Port 8080 was already in use error and how to fix it.

Web server failed to start. Port 8080 was already in use

In the network, an IP address identifies each machine. Similarly, The network port identifies the application or process running on a machine. When an application wants to use a port, the OS binds one. However, when multiple applications want to use the same port, there is a port clash.

In our case, port 8080 was already being used by another application and hence the web server failed to start. Usually, you would get this error in the case of ports 8080, 8081, 9090, etc. So in general, If you get a “port 8080 was already in use” error, then it is certain that another application is already using that port.

This is most likely due to bad configuration from your end, running the application multiple times, or not using proper startup and shutdown scripts.

Fix for Web server failed to start

As we know, The cause is that another process is running on the same port. To solve this, you have two options.

  1. Try to run the application on a port other than 8080.
  2. Identify and stop the process running on that specific port

Option 1: Run your web server on a different port

Most of the application frameworks provide options to change the ports they listen to. For instance, you can change the application port of a spring boot application in the following ways.

You can provide a server.port configuration with a different port number in the application.properties file.

server.port=9090Code language: Properties (properties)

You can also pass the port number as an application parameter.

java - jar my-server.jar --server.port=9090Code language: Bash (bash)

Or a JVM parameter.

java - jar -Dserver.port=9090 my-server.jarCode language: Bash (bash)

This way, The application starts on a different port. Thus the “Web server failed to start. Port 8080 was already in use” error is avoided.

There is a detailed post on how to change the default tomcat port number in 5 different ways. You can also find more details on how the spring boot configuration works in their official documentation.

Option 2: Kill the server running on port 8080

Sometimes, the other process is just an old instance of the same application or an application that you don’t want to run. In these cases, it is best to identify and kill them so that you can start your application on that specific port. To do that you need to first identify the process. Second, you need to kill it.

Say you got the error for running on port 8080. Then you should use the below command for identifying the process or service.

Finding and killing process running on port 8080 on Linux/Mac

One of the following commands should give the process ID (PID) of the application or service running on port 8080.

sudo lsof -n -i :8080 | grep LISTEN
sudo netstat -nlp | grep :8080
sudo ss -lptn 'sport = :8080'Code language: Bash (bash)
fix for Web server failed to start. Port 8080 was already in use
python http server running on port 8080 with PID 25321

With the PID from the above output, you can kill the process using the following commands.

#to kill process gracefully
kill -15 25321Code language: Bash (bash)

or

#To force kill a process
kill -9 25321Code language: Bash (bash)

By killing the process occupying the port on 8080, the web server can start without any problems.

For your use, replace the process id with the process id that you found on your machine.

Finding and killing process running on port 8080 on windows

Similarly, You can run the following command to identify a process running on a port in windows.

netstat -ona | findstr :8080 | findstr LISTENINGCode language: Bash (bash)
command to find process running on port 8080 in windows
process 23928 listening on port 8080

Once you get hold of the process id, you can use the following command to kill it.

taskkill /PID 25321 /F Code language: Bash (bash)

As I said earlier, for your machine, the process ID may be different. The /F is there to force kill the process. In most cases, you will not require this flag.

Conclusion

So we learned ports work and how to solve “Port 8080 was already in use” errors on application startup. If you liked this article, then you might like to read about the following titles.

Понравилась статья? Поделить с друзьями:
  • Ошибка pop3 протокола порт 110
  • Ошибка prl на рефрижераторе zanotti как устранить
  • Ошибка prison break the conspiracy
  • Ошибка pointblank is running already
  • Ошибка printui dll установка принтера