taskkill to differentiate 2 images by path

前端 未结 5 1914
再見小時候
再見小時候 2021-01-04 01:30

How to kill a process by name and orginiated from a particular path using taskkill?

taskkill /F /IM

certainly it cant differentiate 2 process started from t

相关标签:
5条回答
  • 2021-01-04 02:18

    Based on Joey's answer:

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

    This way I avoid NullReferenceException when Path is null (don't know why) and does not need PowerShell.

    Ref: https://superuser.com/questions/52159/kill-a-process-with-a-specific-command-line-from-command-line

    0 讨论(0)
  • 2021-01-04 02:18

    Based on Joey's answer and user's comment.

    Assuming that the path of the program is C:\Dir1\file.exe. If multiple instances of the program are running, you should use the following command:

    Get-WmiObject Win32_Process |
        Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
            ForEach-Object { $_.Terminate() }
    

    Otherwise, Powershell will report an error:

    PS > (Get-WmiObject Win32_Process |
        Where-Object { $_.Path -eq "C:\Dir1\file.exe" }).Terminate()
    
    Method invocation failed because [System.Object[]] doesn't contain a method named 'Terminate'.
    

    In addition, the above command also avoids the error when no matching process is found (e.g., the Path of no process is equal to C:\Dir1\file.exe):

    PS > (Get-WmiObject Win32_Process |
        Where-Object { $_.Path -eq "C:\Dir1\file.exe" }).Terminate()
    
    You cannot call a method on a null-valued expression.
    

    If you don't like WMI:

    Get-Process |
        Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
            ForEach-Object { Stop-Process -Id $_.Id }
    

    I noticed that the Win32_Process.Terminate() and Stop-Process methods are used to forcibly terminate the process, so the process may not be able to perform any cleanup work.

    Tested in Powershell 2.0 on Windows 7 SP1.

    0 讨论(0)
  • 2021-01-04 02:20

    Use the following command (it works even without powershell):

    wmic process where ExecutablePath='C:\\Dir1\\image.exe' delete
    

    NOTE: ExecutablePath is accessable for all processes only if you run wmic as administrator on Windows 8

    0 讨论(0)
  • 2021-01-04 02:28

    Your case seems to be when you have custom services with same process name installed on the machine from different paths. If this is indeed the scenario, you probably have different Service Names which can be used as an additional filter.

    See Syntax:

    taskkill /S {REPLACE_WITH_SERVER_IP_OR_NAME} /F /FI "IMAGENAME eq {REPLACE_WITH_PROCESS_NAME}" /FI "SERVICES eq {REPLACE_WITH_SERVICENAME}"
    

    See Example:

    taskkill /S 10.10.1.1 /F /FI "IMAGENAME eq tomcat7.exe" /FI "SERVICES eq tomcatServiceEngine"
    

    For list of all available filters, please visit taskkill command

    0 讨论(0)
  • 2021-01-04 02:33

    taskkill cannot do it. But you could use PowerShell if it's an option:

    (Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
    
    0 讨论(0)
提交回复
热议问题