Kill Random Process with Name

Kill random process with name

Bash one-liner :-p

kill `ps auxww | grep zsh | awk '{print $2}' | while read line; do echo "$RANDOM $line"; done | sort | cut -d ' ' -f 2 | head -n 1`

How to kill all processes with a given partial name?

Use pkill -f, which matches the pattern for any part of the command line

pkill -f my_pattern

Just in case it doesn't work, try to use this one as well:

pkill -9 -f my_pattern

how kill process with random name?

See Taskkill /?

Taskkill supports the Wildchar * character

ShellExecute(NULL, L"open", L"C:\\WINDOWS\\system32\\cmd.exe", L"taskkill /f /im \"Protector-*\"", NULL, SW_SHOWNORMAL);

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

Batch to kill a process either by PID or process name, depending on user input

Try this:

@echo off
title TASKKILL
tasklist
echo _____________________________________________________________
echo Press 1 to choose PID
echo Press 2 to choose Process Name
set /p option=
if "%option%"=="1" goto :id
if "%option%"=="2" goto :proc

goto :out
:id
set /p pid="Enter the PID of the process you want to kill: "
echo %pid%
taskkill /PID %pid% /F

goto :out
:proc
set /p processName="Enter the name of the process you want to kill: "
echo %processName%
taskkill /IM %processName%.exe /F

:out
pause

You need to avoid the functions in the normal flow, by putting goto :eof or goto :outbranch before them.

So my change here was to add the :out label, and add goto :out before those two functions.

Note when using if to check variable's value, it's better to quote both the variable and the value you are checking, and leave no spaces before and after ==, like this:

if "%var%"=="value" echo "It's euqal!"

So I changed the if %option% == 1 part :)

Linux kill process by cmdline (multiple with same name)

like this:

ps -ef | grep '[S]CREEN -dmSL steambot mono SteamBot\.exe' |
awk '{print $2}' | xargs -r kill

How do I match and kill a specific process without accidentally including unrelated processes?

pgrep / pkill are the best tools to use in this case, instead of ps:

 pgrep -x event-ws   # match by executable filename 'event-ws'; print PID
pkill -x event-ws # match and kill

Each command matches process(es) whose executable filename is event-ws exactly (-x) (irrespective of whether a directory path prefix was used when the executable was launched).

Note, however, that your pgrep / pkill implementation may limit the name to 15 characters - both when matching and in its output.

pgrep simply prints the matching PID(s) (process ID(s)), whereas pkill kills the matching process(es).


If, by contrast, you need to match by a part of the full command line, use the -f option with a regular expression:

pgrep -f '/event-ws/' # match by part of the full command line; print PID and name
pkill -f '/event-ws/' # match and kill

If you add -l to the pgrep command, the full command line of matching process(es) is printed instead of just the process name.



Related Topics



Leave a reply



Submit