cclgoo 发表于 2022-4-14 06:13:36

查找进程PID并自动通过PID结束进程

本帖最后由 cclgoo 于 2022-4-14 07:31 编辑

最近碰到一个程序居然无法通过taskkill /f /t /im xxx.exe来结束进程
想用PID形式结束但PID每次都是变动的,网上很多写法都是查找PID然后,,,眼看,手动!~
翻烂了互联网,找到了蜗牛大人
解释说明:
bat根据exe行程查询进程,对比脚本执行目录。获取当前脚本目录下的xxx.exe的进程ID
wmic process where name="xxx.exe" get processid,executablepath| findstr /C:%url%
注意在bat中 for中的 wmic语句要么使用“”将语句括起来或者使用^符号将“=”,“,”,“|”符号转义一下,否则在执行时会作为分隔符将语句分割而导致报错。如:
“wmic process where name="xxx.exe" get processid,executablepath| findstr /C:%url%”

wmic process where name^="xxx.exe" get processid^,executablepath^| findstr /C:%url%
tokens=1,2 代表获取第一列 第二列的数据 tokens=1-5 获取1到5列的数据
通过taskkill /f /pid pid关闭这个进程
@ping 127.0.0.1 -n 3 >nul 直行等待3s

@echo off
set url_all=""
set pid=0
set url="%~dp0\xxx.exe"
for /f "tokens=1,2" %%a in ('"wmic process where name="xxx.exe" get processid,executablepath| findstr /C:%url%"') do (
      set url_all=%%a
      set pid=%%b
      echo %%a
      echo %%b
)

taskkill /f /pid %pid%

@ping 127.0.0.1 -n 3 >nul
set path=%~dp0
start %path%xxx.exe
pause来个简化的
@echo off
for /f "tokens=2" %%a in ('tasklist^|find /i "xxx"') do (set pid=%%a)
echo "%PID%"
taskkill /f /pid %pid%
pause
再一个
@echo off
for /f "tokens=2 delims=," %%a in ('tasklist /fo csv^|findstr /i /c:"xxx.exe"') do set PID=%%a
echo PID为:%PID:~1,-1%
taskkill /f /pid %pid%
pausebat 搜索进程名并kill
@echo off

set/p "target=nginx(进程名:这里以nginx为例)"
if not defined target (
set "target=nginx"
)
set "list=tasklist /fi "imagename eq %target%.exe""
%list%

set/p "yn=是否继续Y/N(默认Y): "
set "Yy=Y y"
if not defined yn (
set "yn=Y"
)
for %%a in (%Yy%) DO (
if "%%a"=="%yn%" (
for /f "tokens=2 " %%y in ('%list% /nh') do (
REM echo %%y
taskkill /F /PID %%y
)
)
)

set target=
set yn=
set Yy=
set list=
pause

nttwqz 发表于 2022-4-14 07:50:47

N年前好像已经讨论过,你搜搜

极冰凌心 发表于 2022-4-14 08:32:17

一些权限高的有没有办法关掉呢

cclgoo 发表于 2022-4-14 08:47:10

本帖最后由 cclgoo 于 2022-4-14 08:50 编辑

极冰凌心 发表于 2022-4-14 08:32
一些权限高的有没有办法关掉呢
不是权限的问题,不知道什么原因,有些进程不知道为什么在WIN10上杀不了,进程和ID方式都不行!~我在找其它方式!~弄了两个关联在一起的BAT组合杀才能干掉!~

cclgoo 发表于 2022-4-14 08:48:31

nttwqz 发表于 2022-4-14 07:50
N年前好像已经讨论过,你搜搜

不好意思,新人,挠破头皮才找的到一点点线索!~

旁观者清 发表于 2022-4-14 09:14:04

谢谢分享。

being 发表于 2022-4-15 15:04:41

taskkill /f /im与taskkill /f /pid 力度是一样的吧
页: [1]
查看完整版本: 查找进程PID并自动通过PID结束进程