有没有办法通过知道服务的名称在批处理脚本中使用命令获取Windows服务的PID?
答案 0 :(得分:16)
请尝试以下代码:
FOR /F "tokens=3" %%A IN ('sc queryex %serviceName% ^| findstr PID') DO (SET pid=%%A) IF "!pid!" NEQ "0" ( taskkill /F /PID !pid! )
答案 1 :(得分:3)
@echo off
for /f "tokens= delims=" %%# in ('
wmic service where "name='Service'" get ProcessId /format:value
') do (
for /f "tokens=* delims=" %%$ in ("%%#") do set "%%$"
)
taskkill /pid %ProcessId% /f
答案 2 :(得分:3)
只需taskkill /f /fi "SERVICES eq <service_short_name>"