windows 如何从按用户名和图像名过滤的命令行获取 PID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8435952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:59:58  来源:igfitidea点击:

how to get PID from command line filtered by username and imagename

windowscommand-linepid

提问by NOTACAT Studio

I need to be able to get the PID from a running process (cmd.exe) using the command line. The problem is there are two cmd.exe running. One is under the username SYSTEM and one is compUser. Is there a way that I can grab the PID of the compUser cmd.exe?

我需要能够使用命令行从正在运行的进程 (cmd.exe) 中获取 PID。问题是有两个 cmd.exe 正在运行。一种在用户名 SYSTEM 下,一种是 compUser。有没有办法可以获取 compUser cmd.exe 的 PID?

Edit: This needs further explanation. I'm doing this from a batch file. One of the calls that I'm making in my batch file starts a cmd.exe that never dies. So killing that cmd.exe would be simple:

编辑:这需要进一步解释。我正在从批处理文件中执行此操作。我在批处理文件中进行的调用之一启动了一个永不消亡的 cmd.exe。所以杀死那个 cmd.exe 很简单:

taskkill /F /IM cmd.exe /FI "username eq compUser"

The problem is that the batch file that I'm in is being handled by another instance of cmd.exe under the username compUser. What I'm attempting to do is get the PID from the original cmd.exe before I start the second cmd.exe. That way I can just use the command:

问题是我所在的批处理文件正在由用户名 compUser 下的 cmd.exe 的另一个实例处理。我试图做的是在启动第二个 cmd.exe 之前从原始 cmd.exe 获取 PID。这样我就可以使用命令:

taskkill /F /IM cmd.exe /FI "username eq compUser" /FI "PID neq [orignal task's PID]"

采纳答案by NOTACAT Studio

The way I ended up having to do this was use:

我最终不得不这样做的方式是使用:

TASKLIST /NH /FI  "IMAGENAME eq cmd.exe" /FI "username eq compUser"> psid.txt
FOR /F "tokens=2" %%I in (psid.txt ) DO set pIdNotToKill=%%I

right before I started the batch script that hangs. Then when I was ready to kill the hanging cmd window:

就在我开始挂起的批处理脚本之前。然后当我准备杀死悬挂的 cmd 窗口时:

taskkill /F /IM cmd.exe /FI "PID ne %pIdNotToKill%" /FI "username eq compUser"

There is probably a better way, but this worked.

可能有更好的方法,但这有效。

回答by telenachos

This will display all processes named "cmd.exe" for the user "compUser":

这将显示用户“compUser”的所有名为“cmd.exe”的进程:

tasklist /FI "IMAGENAME eq cmd.exe" /FI "USERNAME eq compUser"

回答by SiB

I set the Title to my own cmd window, other cmd windows what I call or run by "start /b" I rename to other window title name. Next I detect PID, set MYPID and kill all other cmd with OTHER PID of my.

我将标题设置为我自己的 cmd 窗口,其他 cmd 窗口我通过“start /b”调用或运行我重命名为其他窗口标题名称。接下来我检测 PID,设置 MYPID 并使用我的 OTHER PID 杀死所有其他 cmd。

All on one file, working in other label loop with timeout dalay.

所有在一个文件上,在其他标签循环中工作,超时时间。

@(@Title TitleOfMyScript)
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq cmd.exe" /fi "windowtitle eq TitleOfMyScript" /nh') do set MYPID=%%a
taskkill /FI "PID ne %MYPID%" /FI "IMAGENAME eq cmd.exe" /F

回答by Igor Bljahhin

I'd like just to share my piece of code, maybe it will be useful for somebody. It is based on the comment from jiggawagga, but it can terminate many processes:

我只想分享我的一段代码,也许它对某人有用。它基于 jiggaagga 的评论,但它可以终止许多进程:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION

REM kill old server process
SET pIdNotToKill=
SET pIdFilter=

FOR /F "tokens=2" %%I IN (%SERVER_HOME%\psid.txt) DO (
    IF NOT "%%I"=="No" (
        SET pIdNotToKill=!pIdNotToKill! %%I
        SET pIdFilter=!pIdFilter! /FI "PID ne %%I"
    )
)

IF NOT "!pIdNotToKill!"=="" (
    ECHO Killing all Java processes except%pIdNotToKill%
    ECHO TASKKILL command will be called with the filter%pIdFilter%
    TASKKILL /F /IM java.exe %pIdFilter%
)

DEL %SERVER_HOME%\psid.txt

TASKLIST /NH /FI "IMAGENAME eq java.exe" > %SERVER_HOME%\psid.txt

right before I start server process.

在我开始服务器进程之前。