Reputation: 33
I supposed to close a process (java). This process is invoked by a batch file. The batch file title is "Secondary Push"
When i double the batch file, it opens a command prompt and records the log.
-How to close the java process ?? -How to get the process id of the particular java process... using command prompt. Not by Task Manager.
Upvotes: 0
Views: 3632
Reputation: 80113
Tasklist
will show you the details.
@ECHO OFF
SETLOCAL
SET "underline="
SET "mypid="
FOR /f "skip=2delims=" %%u IN ('tasklist/v') DO IF NOT DEFINED underline SET underline=%%u
FOR /f "delims=" %%u IN ('tasklist/v^|find /i "GPU Client"') DO IF NOT DEFINED mypid SET mypid=%%u
:loop
IF "%underline:~0,1%"=="=" SET underline=%underline:~1%&SET mypid=%mypid:~1%&GOTO loop
FOR %%u IN (%mypid%) DO IF DEFINED underline SET mypid=%%u&SET "underline="
echo Target process ID=%mypid%
GOTO :EOF
This should get the process ID ready for TASKKILL
. The string GPU Client
should be replaced by a uniwue string identifying the java process that you wish to terminate, which you should be able to derive from a tasklist
listing from the prompt.
tasklist /v
The length of the underline appearing under the tasklist /v
report heading varies depending on the length of the longest name of tasks currently running, so the underline line is applied to underline
, the selected detail line to mypid
and then the first character of each string is trimmed off until the space in the line-of-=
is found.
At this time, mypid
will have the taskname trimmed off, so its first token is the PID.
Upvotes: 3