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

后端 未结 4 1363
广开言路
广开言路 2020-12-06 14:45

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

4条回答
  •  时光取名叫无心
    2020-12-06 15:17

    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:

    @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.

提交回复
热议问题