Kill Process by Name

How can I kill a process by name instead of PID, on Linux?

pkill firefox

More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm

How to kill a process by name?

You can use this function in order to kill a process by name:

uses
TlHelp32;

function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

Note:

  • There could be more than 1 process having the same name
  • KillTask function returns the count of the killed processes
  • The page where I've found the function says that it works on Windows 9x/ME/2000/XP.
  • I've personally tested it on Windows 7/10

how to kill a program by process name using cmd in windows 8?

You can kill the process and all its children (ie. all processes started by it) using the /t switch:

taskkill /f /t /im wwahost.exe

See the documentation for taskkill (ss64) and tskill (ss64) system utilities.

How to kill all processes matching a name?

From man 1 pkill

-f     The pattern is normally only matched against the process name.
When -f is set, the full command line is used.

Which means, for example, if we see these lines in ps aux:

apache   24268  0.0  2.6 388152 27116 ?        S    Jun13   0:10 /usr/sbin/httpd
apache 24272 0.0 2.6 387944 27104 ? S Jun13 0:09 /usr/sbin/httpd
apache 24319 0.0 2.6 387884 27316 ? S Jun15 0:04 /usr/sbin/httpd

We can kill them all using the pkill -f option:

pkill -f httpd

Kill 2 processes with the same name per user by cmd

Refer to @MC ND's Answer here Kill process in batch file that matches certain name and user

Just test if any line in tasklist output passes the two filters, account and executable.

If no process is found, continue

If process is found, anotate it and increment the loop counter

If the limit is reached, kill the process else wait and repeat the process



@ECHO off
Set "MyProcess=wscript.exe"
set "loop=0"
set "loopLimit=2"
:PRVARCHSTART
set "process="
@for /f "tokens=2 delims=," %%a in (
'Tasklist /v /fo:csv /nh ^| findstr /l /c:"%username%" ^| findstr /l /c:"%MyProcess%"'
) do (
set /a "loop+=1"
set "process=%%~a"
)

if not defined process goto :PRVARCHEND

if %loop% geq %loopLimit% (
Taskkill /pid %process% /f
goto :PRVARCHEND
)
Ping -n 1 localhost >nul 2>nul
goto :PRVARCHSTART
:PRVARCHEND
pause & Exit

I have an old vbscript that may be help you to choose what Process you want to kill by its command line !

Option Explicit
Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,_
"There is an existing proceeding !"
WScript.Quit
Else
Copyright = "[Hackoo "& chr(169) & " 2015]"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject( "Wscript.Shell" )
NomFichierLog="Killed_Process.txt"
temp = ws.ExpandEnvironmentStrings("%temp%")
PathNomFichierLog = temp & "\" & NomFichierLog
Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
Call Find("wscript.exe")
Call Find("cmd.exe")
Call Explorer(PathNomFichierLog)
End If
'***************************************************************************************************
Function Explorer(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run "Explorer "& File & "\",1,True
end Function
'***************************************************************************************************
Sub Find(MyProcess)
Dim colItems,objItem,Processus,Question,Msg
Titre = " Process(es) "& DblQuote(MyProcess) &" running "
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like " & CommandLineLike(WScript.ScriptFullName) & "",,48)
Count = 0
For Each objItem in colItems
Count= Count + 1
Processus = objItem.CommandLine
Question = MsgBox ("Would do you like to kill this script : " & DblQuote(Processus) & " ?" ,VBYesNO+VbQuestion,Titre+Copyright)
If Question = VbYes then
objItem.Terminate(0)'To kill the process
OutPut.WriteLine Processus
else
Count= Count - 1 'decrementing the count of -1
End if
Next
OutPut.WriteLine String(100,"*")
If Count > 1 Then
Msg = " were killed"
Else
Msg = " was killed"
End if
OutPut.WriteLine count & Titre & Msg
OutPut.WriteLine String(100,"*") & VbCrLF
End Sub
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'**************************************************************************


Related Topics



Leave a reply



Submit