sridattas
sridattas

Reputation: 509

How to fetch the pid and kill it using batch file?

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

C:\Users\k9>ps -ef|grep java
  10892   5648  0   Mar 08 con  0:02 "C:\Program Files\Java\jdk1.7.0_51\bin\java"  com.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.

Upvotes: 1

Views: 5343

Answers (2)

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 1

blaze_125
blaze_125

Reputation: 2317

This is how I "find" and kill Adobe Acrobat

::Close acrobat and any opened PDF
taskkill /im acrobat.exe /t /f 
timeout 2

/im for imagename, or program you're targeting
/t terminates the specified process and any child process which were started by it
/f Forcefully terminate the process

Upvotes: 3

Related Questions