Home > Enterprise >  How can I clear the process running on my target port?
How can I clear the process running on my target port?

Time:12-08

FINAL UPDATE: Ture Pålsson SOLVED MY ISSUE. THANKS SO MUCH ALL FOR YOUR HELP!

I've been following along with this article, trying to create my first Flask API from scratch. I had to update my computer in the middle of my work and I don't think I successfully closed the terminal beforehand.

When I try to run my application now from the home directory of my project, in the section titled "Creating a RESTful Endpoint with Flask", I get a long stack trace ending with this:

  File "/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 48] Address already in use

Other similar questions which indicated this is due to another process using the port I'm trying to access. To solve this, I've used the command:

ps -fA | grep python

Which returns:

501 12797 12759   0 11:15PM ??         0:03.97 /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer) --ms-enable-electron-run-as-node /Users/nfeldman1995/.vscode/extensions/ms-python.vscode-pylance-2021.12.0/dist/server.bundle.js --cancellationReceive=file:50e28be741e040fa2f7efc8dbdcf8a4ca200512639 --node-ipc --clientProcessId=12759
501 13126 12421   0 11:30PM ttys000    0:00.00 grep python

This is what I find so confusing. Other articles indicate I should see a line with the output along the lines of:

 502 89332 12877 0 3:40PM ttys00 0:00.15 python -m SimpleHTTPServer

However, nothing in this output seems to be running "SimpleHTTPServer" so I don't know what process to kill. I've tried killing both of the other tasks with the command kill <process#> but this hasn't solved the problem.

Going to localhost:5000 (The port I'm trying to target) gives me a 404 error.

UPDATE: Here is the output for lsof -i :5000

COMMAND     PID         USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
ControlCe 13218 nfeldman1995   27u  IPv4 0xdf2d97d6ad526f5      0t0  TCP *:commplex-main (LISTEN)
ControlCe 13218 nfeldman1995   28u  IPv6 0xdf2d97d52942a1d      0t0  TCP *:commplex-main (LISTEN)

I tried running kill -9 13218 and running my application again and got the same error. Running lsof, then killing the process (or so I thought), then running lsof again seems to indicate new processes keep getting created. (With different process ID numbers).

Here is my bootstrap.sh file, as requested by Jon:

#!/bin/sh
export FLASK_APP=./cashman/index.py
source $(pipenv --venv)/bin/activate
flask run -h 0.0.0.0

CodePudding user response:

The problem with port 5000 on newer versions of macOS is that AirPlay uses it; that's why you see "ControlCenter" listening on the port. To get rid of it, go into System settings and turn off "AirPlay receiver" or something like that (I can't tell exactly; I'm on a too old computer at the moment). Googling "macos port 5000" turns up several hits.

CodePudding user response:

You can use lsof to list the processes, their PIDs and the TCP ports they are listening on with a command like this:

sudo lsof -iTCP -sTCP:LISTEN -P -n
  • Related