根据端口号kill掉进程

场景:

“一个cmd窗口运行任务,但不会停止,需要在其执行一段时间后自动停止”,引入这个.bat脚本,kill掉固定端口对应的进程。具体如下:

setlocal enabledelayedexpansion
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":8000"') do (
if "%%e%" == "" (
set pid=%%d
) else (
set pid=%%e
)
)
echo !pid!
taskkill /f /pid !pid!
exit

涉及命令:

获取端口号对应的进程:netstat -ano|findstr [port]

获取进程名称:tasklist|findstr [pid]

kill掉进程:taskkill /f /pid !pid!

猜你喜欢

转载自www.cnblogs.com/jinziguang/p/11647669.html