I have many executable jar files which I am running in a windows server. Sometime, I need to redeploy some of the applications. For that, I need to first stop the existing process and then start the new process.
In linux environment I am using pkill
to kill the process based on the jar file name. But in windows, all the java processes will be named as "java.exe" only. I need to write a batch file to do this start and stop operations.
How can I kill the jar file based on the name.
Assume that my jar file is named as common-api.jar
.
Running the command jps -ml | find "common"
would give the result as
6968 common-api.jar
I can kill the process by taskkill /f /PID 6968
But How can I extract the process id from the first command and pass it to the taskkill
command ?
If you want to kill all java.exe processes : taskkill /F /IM java.exe /T .
start "MyProgramName" java java-program.. Here start command will open a new window and run the java program with window's title set to MyProgramName. Your Java program will be killed only.
Graceful-kill-java allows you to gracefully kill java processes, which 'taskkill pid' and 'wmic ... call terminate' cannot do. TL;DR Use the command graceful-kill-java. bat [command line partial text] to kill all java processes whose command line contains the partial text.
for /f "delims= " %%a in ('jps -ml ^| find /i "common"') do set PID=%%a
taskkill /f /PID %PID%
or from command prompt:
for /f "delims= " %a in ('jps -ml ^| find /i "common"') do set PID=%a
In vbscript we can do something like that just give a try with it :
Option Explicit
Call KillProcessbyName("common-api.jar")
'**********************************************************************************************
Sub KillProcessbyName(FileName)
On Error Resume Next
Dim WshShell,strComputer,objWMIService,colProcesses,objProcess
Set WshShell = CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
For Each objProcess in colProcesses
If InStr(objProcess.CommandLine,FileName) > 0 Then
If Err <> 0 Then
MsgBox Err.Description,VbCritical,Err.Description
Else
objProcess.Terminate(0)
End if
End If
Next
End Sub
'**********************************************************************************************
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With