Last active
August 29, 2015 13:56
-
-
Save elw00d/9174423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Windows equivalent of pkill command for java-processes. Add JDK bin directory to PATH environment variable. Usage:
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.
If ambigious, first found process will be killed without confirmation. Remaining processes won't be touched.
User will be asked to confirm to kill all matching processes.
All matching processes will be killed without any confirmation.