如何删除已分配给端口的当前进程/应用程序?

例如:localhost:8080


当前回答

使用GitBash的一行解决方案:

 tskill `netstat -ano | grep LISTENING | findstr :8080 | sed -r 's/(\s+[^\s]+){4}(.*)/\1/'`

将8080替换为服务器正在侦听的端口。

如果您需要经常使用它,请尝试添加到~/。Bashrc函数:

function killport() {
        tskill `netstat -ano | findstr LISTENING | findstr :$1 | sed -r 's/^(\s+[^\s]+){4}(\d*)$/\1/'`
}

然后简单地运行

killport 8080

其他回答

如果你想使用Python:检查是否可以在Python中杀死正在监听特定端口的进程,例如8080?

Smunk给出的答案很好。我在这里重复他的密码:

from psutil import process_iter
from signal import SIGTERM # or SIGKILL

for proc in process_iter():
    for conns in proc.connections(kind='inet'):
        if conns.laddr.port == 8080:
            proc.send_signal(SIGTERM) # or SIGKILL
            continue

我知道这是一个很老的问题,但发现很容易记住,快速命令杀死使用端口的应用程序。

要求:npm@5.2.0^版本

npx kill-port 8080

你也可以在这里阅读更多关于kill-port的内容:https://www.npmjs.com/package/kill-port

第一步

netstat -vanp tcp | grep 8888

例子

tcp4     0      0    127.0.0.1.8888   *.*    LISTEN      131072 131072  76061    0
tcp46    0      0    *.8888           *.*    LISTEN      131072 131072  50523    0

第二步:找到你的pid并杀死他们

对我来说

sudo kill -9 76061 50523

第一步(与KavinduWije所写的接受答案相同):

netstat -ano | findstr:yourPortNumber

第2步更改为:

tskill typeyourPIDhere 

注意:taskkill在某些git bash终端中不起作用

netstat -ano | findstr :PORT
kill PI