如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
当前回答
下面是在WSL2中执行此操作的脚本
PIDS=$(cmd.exe /c netstat -ano | cmd.exe /c findstr :$1 | awk '{print $5}')
for pid in $PIDS
do
cmd.exe /c taskkill /PID $pid /F
done
其他回答
如果你使用的是Windows终端,那么杀死过程可能不那么乏味。 我一直在使用windows终端,kill PID对我来说可以很好地杀死端口上的进程,因为新的windows终端支持某些bash命令。例如:杀死13300人
整个过程是这样的
打开Windows终端 键入以下命令以显示在您希望终止进程的端口上运行的进程。 netstat -ano | findstr:PORT 输入following终止进程。 杀死PID
例如:
PS C:\Users\username> netstat -ano | findstr :4445
TCP 0.0.0.0:4445 0.0.0.0:0 LISTENING 7368
TCP [::]:4445 [::]:0 LISTENING 7368
PS C:\Users\username> kill 7368
PS C:\Users\username> netstat -ano | findstr :4445
PS C:\Users\username>
当我输入第一个命令来列出端口上的进程时,它返回空。这意味着现在所有进程都被杀死了。
更新:kill是Stop-Process的别名。谢谢@FSCKur让我们知道。
如果你使用powershell 7+,这对我有用。只需在$PROFILE文件中添加此函数。
function killport([parameter(mandatory)] [string] $uport){
if($upid = (Get-NetTCPConnection -LocalPort $uport -ErrorAction Ignore).OwningProcess){kill $upid}
}
然后简单地使用killport 8080
或者如果你只喜欢命令,你可以试试这个:
kill $(Get-NetTCPConnection -LocalPort 8761 -ErrorAction Ignore).OwningProcess
我在Windows上运行zookeeper,无法使用zookeeper-stop.sh停止在2181端口运行的zookeeper,所以尝试了这个双斜线“//”方法来kill任务。它工作
1. netstat -ano | findstr :2181
TCP 0.0.0.0:2181 0.0.0.0:0 LISTENING 8876
TCP [::]:2181 [::]:0 LISTENING 8876
2.taskkill //PID 8876 //F
SUCCESS: The process with PID 8876 has been terminated.
如果你想使用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
在Windows PowerShell版本1或更高版本中停止端口3000上的进程:
Stop-Process (,(netstat -ano | findstr:3000).split() | foreach {$[$.length-1]}) -Force
根据@morganpdx的建议,这里有一个更像powershell的更好的版本:
Stop-Process -Id (get - netttcpconnection -LocalPort 3000)。OwningProcess force