Kill a process with a specific "Command Line" from command line

24

12

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

Posted 2009-10-07T10:16:43.333

Reputation: 9 293

3I believe both the answers below are wrong, as you arent just asking how to kill a .exe process, you are asking how to kill a .exe process which contains a specific command line – admintech – 2009-10-07T10:45:11.577

2Are you only talking about Windows? Your examples and the supplied answers make it seem like you are, but you didn't specify this. – Nathan Fellman – 2009-10-07T10:45:54.897

Can you explain "how this is possible through Process Explorer?" I just launched a java - jar<app-name> and it shows only java.exe – Sathyajith Bhat – 2009-10-07T12:00:25.760

i bet he/she meant "sysinternals process explorer" – akira – 2009-10-07T12:35:52.037

Sysinternals Process Explorer, of course. You can view much information about running processes from it, including their command line. – ripper234 – 2009-10-07T14:22:41.237

I was referring to Sysinternals' process explorer, as well – Sathyajith Bhat – 2009-10-07T15:26:26.607

Answers

34

In Windows XP, you can do this easyly uing WMIC, the WMI Console. From a command propt, 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. This alias may not be declared on every OS.

Benoit

Posted 2009-10-07T10:16:43.333

Reputation: 590

3Just a little tip for cmd files - to use this command from cmd file you should replace escape all '%' chars with a second '%' char, e.g. ... CommandLIne Like '%%-jar ... – sarh – 2014-11-19T16:12:11.170

1+20 That's it! Dammit :) I too was following the WMIC. But I was doing it from within the WMIC console and wasn't being able to apply LIKE. Was getting syntax errors, which were forcing me to use '=', which in turn forced me to input the whole CommandLine field. Glad to know LIKE works outside the WMIC console. Should have thought of that. Kudos to you – A Dwarf – 2009-10-07T13:04:39.827

works perfectly when I call it from command line. I have TeamCity starting a process which I need to kill at the end of the build. Somehow when the same command line called by TeamCity it returns "No Instance(s) Available", the same commad like copied/pasted to cmd kills the process correctly. Any ideas why would that be? – root – 2014-06-06T14:00:57.593

8

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

Posted 2009-10-07T10:16:43.333

Reputation: 1 978

3

Simple one-liner in powershell:

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

wisbucky

Posted 2009-10-07T10:16:43.333

Reputation: 1 522

I should really learn PS sometime. – ripper234 – 2017-01-16T22:46:19.917

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

Posted 2009-10-07T10:16:43.333

Reputation: 206

I tried it with notepad, but the startinfo.arguments were blank. – js2010 – 2019-01-31T17:18:15.320

2

Powershell:-

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

Richard Forrester

Posted 2009-10-07T10:16:43.333

Reputation: 21

1

I use a variation of Brain'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))..."
    }
}

Jan H

Posted 2009-10-07T10:16:43.333

Reputation: 171

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

Posted 2009-10-07T10:16:43.333

Reputation: 321

Could you explain the difference between your PS command and the others here? – Burgi – 2019-02-01T16:27:46.930

It's basically the same. Perhaps easier to type and remember. -match can actually take a regular expression. – js2010 – 2019-02-01T16:32:24.600

You should [edit] your answer to include that... – Burgi – 2019-02-01T16:43:54.063

-2

Use the free PsKill:

pskill java.exe

harrymc

Posted 2009-10-07T10:16:43.333

Reputation: 306 093

1you missed the 2nd part of the question: "specific commandline"... not the first java.exe, that comes along .. neither all java.exe processes – akira – 2009-10-07T11:33:19.200