简体   繁体   English

如何清除目标端口上运行的进程?

[英]How can I clear the process running on my target port?

FINAL UPDATE: Ture Pålsson SOLVED MY ISSUE.最后更新:Ture Pålsson 解决了我的问题。 THANKS SO MUCH ALL FOR YOUR HELP!非常感谢您的帮助!

I've been following along with this article , trying to create my first Flask API from scratch.我一直在关注这篇文章,尝试从头开始创建我的第一个 Flask API。 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:当我现在尝试从我的项目的主目录运行我的应用程序时,在标题为“使用 Flask 创建 RESTful 端点”的部分中,我得到了一个长的堆栈跟踪,以这个结尾:

  File "/usr/local/Cellar/python@3.9/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:其他文章表明我应该看到一条与 output 沿线的线:

 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.但是,这个 output 似乎没有运行“SimpleHTTPServer”,所以我不知道要杀死哪个进程。 I've tried killing both of the other tasks with the command kill <process#> but this hasn't solved the problem.我尝试使用命令kill <process#>其他两个任务,但这并没有解决问题。

Going to localhost:5000 (The port I'm trying to target) gives me a 404 error.转到 localhost:5000 (我试图定位的端口)给我一个 404 错误。

UPDATE: Here is the output for lsof -i:5000更新:这是 lsof -i:5000 的 output

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.我尝试运行kill -9 13218并再次运行我的应用程序并得到相同的错误。 Running lsof, then killing the process (or so I thought), then running lsof again seems to indicate new processes keep getting created.运行 lsof,然后杀死进程(或者我认为),然后再次运行 lsof 似乎表明新进程不断被创建。 (With different process ID numbers). (使用不同的进程 ID 号)。

Here is my bootstrap.sh file, as requested by Jon:这是我的 bootstrap.sh 文件,应 Jon 的要求:

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

The problem with port 5000 on newer versions of macOS is that AirPlay uses it;较新版本的 macOS 上端口 5000 的问题是 AirPlay 使用它; that's why you see "ControlCenter" listening on the port.这就是为什么您会看到“ControlCenter”在端口上侦听。 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).为了摆脱它,go 进入系统设置并关闭“AirPlay接收器”或类似的东西(我不能确切地说;我现在用的电脑太旧了)。 Googling "macos port 5000" turns up several hits.谷歌搜索“macos port 5000”出现了几个点击。

You can use lsof to list the processes, their PIDs and the TCP ports they are listening on with a command like this:您可以使用lsof列出进程、它们的 PID 和它们正在侦听的 TCP 端口,使用如下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM