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

55,497

Solution 1

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.

Solution 2

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.

Solution 3

Simple one-liner in powershell:

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

Solution 4

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)

Solution 5

Powershell:-

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

Related videos on Youtube

ripper234
Author by

ripper234

Updated on September 17, 2022

Comments

  • ripper234
    ripper234 over 1 year

    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.

    • Taken
      Taken over 14 years
      I 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
    • Nathan Fellman
      Nathan Fellman over 14 years
      Are you only talking about Windows? Your examples and the supplied answers make it seem like you are, but you didn't specify this.
    • 100rabh
      100rabh over 14 years
      Can you explain "how this is possible through Process Explorer?" I just launched a java - jar<app-name> and it shows only java.exe
    • akira
      akira over 14 years
      i bet he/she meant "sysinternals process explorer"
    • ripper234
      ripper234 over 14 years
      Sysinternals Process Explorer, of course. You can view much information about running processes from it, including their command line.
    • 100rabh
      100rabh over 14 years
      I was referring to Sysinternals' process explorer, as well
  • akira
    akira over 14 years
    you missed the 2nd part of the question: "specific commandline"... not the first java.exe, that comes along .. neither all java.exe processes
  • A Dwarf
    A Dwarf over 14 years
    +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
  • root
    root almost 10 years
    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?
  • sarh
    sarh over 9 years
    Just 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 ...
  • ripper234
    ripper234 over 7 years
    I should really learn PS sometime.
  • js2010
    js2010 over 5 years
    I tried it with notepad, but the startinfo.arguments were blank.
  • Burgi
    Burgi over 5 years
    Could you explain the difference between your PS command and the others here?
  • js2010
    js2010 over 5 years
    It's basically the same. Perhaps easier to type and remember. -match can actually take a regular expression.
  • Burgi
    Burgi over 5 years
    You should edit your answer to include that...