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