How to recover the pid and kill it using a batch file?

advertisements

i have to find a processid for a specific jar file and kill it using the same batch file.

C:\Users\k98>ps -ef|grep java
  10892   5648  0   Mar 08 con  0:02 "C:\Program Files\Java\jdk1.7.0_51\bin\java"  com.dassault_systemes.mkwebappserver.MainClass -app CATMkWebAppServerConsole\apps_list.html -autobui
  13060   7828  0 09:42:28 con 49:05 java  -Djsse.enableSNIExtension=false -Djava.util.logging.config.file=..\config\log.properties -classpath "../extlib/*";..\extlib\mysql-connector-

here i want to find pid of config\log.properties file and kill it using the batch file.


The most flexible way to detect a pid by its command line (where your jar should be included) is with wmic. To process the wmic result and assign it to a variable you'll need FOR /F :

@echo off

for /f  "useback tokens=* delims=" %%# in (
    `wmic process where "CommandLine like '%%my_jar.jar%%' and not CommandLine like '%%wmic%%' " get  ProcessId /Format:Value`
) do (
    for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)

echo %processId%

echo taskkill /pid %processId% /f

Where you should change '%%my_jar.jar%%' with your jar name.To kill the process you'll have to delete the echo word in the last line. It is echoed in order to check if the correct process id is handled.

For more info:

WMIC

FOR /F

WQL

TASKKILL