Skip to content

Instantly share code, notes, and snippets.

@elw00d
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elw00d/9174423 to your computer and use it in GitHub Desktop.
Save elw00d/9174423 to your computer and use it in GitHub Desktop.
@echo off
setlocal enabledelayedexpansion
IF [%1]==[] (
echo Missing argument
echo Usage: javakill ^<string^> [/A] [/Q]
echo /A - Kill all found processes
echo /Q - Be quite ^(don't ask if found more than one process^)
goto :exit
)
set KILL_ALL=0
set QUITE_MODE=0
if '%2'=='/A' set KILL_ALL=1
if '%2'=='/a' set KILL_ALL=1
if '%3'=='/A' set KILL_ALL=1
if '%3'=='/a' set KILL_ALL=1
if '%2'=='/Q' set QUITE_MODE=1
if '%2'=='/q' set QUITE_MODE=1
if '%3'=='/Q' set QUITE_MODE=1
if '%3'=='/q' set QUITE_MODE=1
echo KILL_ALL=%KILL_ALL%
echo QUITE_MODE=%QUITE_MODE%
echo ---- All java processes: ----
jps -v
echo.
echo ---- Processes matches %1 (only first one will be killed): -----
jps -v | FindStr %1
echo.
set pid=0
set count=0
set KILL_ALL_CONFIRMED=0
rem Take first token from string delimited by whitespace
for /F "tokens=1,2 delims= " %%a in ('jps -v ^| FindStr %1') do (
if !KILL_ALL!==1 (
if !KILL_ALL_CONFIRMED!==0 (
SET /P AREYOUSURE="Are you sure to kill all (Y/[N])?"
IF /I "!AREYOUSURE!" NEQ "Y" goto :exit
set KILL_ALL_CONFIRMED=1
)
echo ---- Killing process %%a %%b ----
taskkill /PID %%a /F
)
if !KILL_ALL!==0 (
if !count!==0 (
set pid=%%a
)
set /a count=count+1
)
)
echo PID=%pid%
echo Count=%count%
if !count!==0 (
echo No processes found for specified query
goto :exit
)
if [!count!] gtr [1] (
if !QUITE_MODE!==0 (
SET /P AREYOUSURE="Ambigious.. are you sure to kill first %pid% (Y/[N])?"
IF /I "!AREYOUSURE!" NEQ "Y" goto :exit
)
echo ---- Killing process %pid% ----
taskkill /PID %pid% /F
)
if !count!==1 (
echo ---- Killing process %pid% ----
taskkill /PID %pid% /F
)
:exit
@elw00d
Copy link
Author

elw00d commented Feb 23, 2014

Windows equivalent of pkill command for java-processes. Add JDK bin directory to PATH environment variable. Usage:

javakill glassfish

Searches java process with JVM options contains 'glassfish' substring and kills them if it is only one. If there are several matching processes, user will be asked to confirm a killing first of them.

javakill glassfish /Q

If ambigious, first found process will be killed without confirmation. Remaining processes won't be touched.

javakill glassfish /A

User will be asked to confirm to kill all matching processes.

javakill glassfish /Q /A

All matching processes will be killed without any confirmation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment