32

Is there a command line utility that kills all processes with a specific command line?

E.g. kill all processes named "java.exe" with a command line that contains "-jar selenium-server.jar". This is possible through process explorer.

ripper234
  • 11,903

8 Answers8

40

In Windows XP you can do this easily using WMIC, the WMI Console. From a command prompt, type the following:

wmic Path win32_process Where "CommandLine Like '%-jar selenium-server.jar%'" Call Terminate

Edit:

I replaced the alias 'process' by it full path ('path win32_process') as is Aviator's port. Note: This alias may not be declared on every OS.

Benoit
  • 650
9

If you are using a Windows version which has WMIC command in it. You can try this

wmic path win32_process Where "Caption Like '%java.exe%' AND CommandLine Like '%selenium.jar%'" get ProcessId|more +1

The more +1 removes first line containing the header and prints the PID alone. If there are more than one java process containing selenium.jar then this will return one PID per line.

vpram86
  • 2,838
6

Simple one-liner in powershell:

(Get-WmiObject win32_process -filter "Name='java.exe' AND CommandLine LIKE '%-jar selenium-server.jar%'").Terminate()
wisbucky
  • 3,346
3

I believe you could do this with PowerShell using Get-Process and the StartInfo.Arguments on the process you want.

$procs = Get-Process java
foreach($proc in $procs) 
{
    if($proc.StartInfo.Arguments -contains "-jar selenium-server.jar")
    {
        kill $proc
    }
}

(I haven't tested that completely, but you should be able to tweak it to make it work)

brien
  • 206
2

Powershell:-

$oProcs = get-process explorer;foreach ($oProc in $oProcs){if ($oProc.Path.Contains('C:\Windows')) {Stop-Process $oProc.Id}}
suspectus
  • 5,008
1

I use a variation of Brien's PowerShell script.

This outputs command line and other info as well.

$processes = Get-WmiObject Win32_Process -Filter "name = 'java.exe'"
foreach($proc in $processes)
{
    if($proc.CommandLine.Contains("selenium-server.jar"))
    {
        Write-Host "stopping proccess $($proc.ProcessId) with $($proc.ThreadCount) threads; $($proc.CommandLine.Substring(0, 50))..."
        Stop-Process -F $proc.ProcessId
    } else
    {
        Write-Host "skipping proccess $($proc.ProcessId) with $($proc.ThreadCount) threads; $($proc.CommandLine.Substring(0, 50))..."
    }
}
briantist
  • 1,131
Jan H
  • 211
0

Another powershell variation. It's basically the same, perhaps easier to type and remember. -match can actually take a regular expression.

get-wmiobject win32_process | where commandline -match selenium-server.jar 
  | remove-wmiobject
js2010
  • 713
-3

Use the free PsKill:

pskill java.exe

harrymc
  • 498,455