Difference between taskkill and taskkill /f - windows

On Microsoft Technet I can read that taskkill has a /f parameter to kill a process forcefully. I wonder what this does internally, to understand the impact of such an action.
taskkill (without /f) does not simply send a WM_CLOSE message to the process, otherwise my application would ask whether or not to save the open documents. This makes me assume that it already operates on a TerminateProcess (MSDN) level. However, TerminateProcess does not have a parameter for forcing a kill.
So, what do taskkill and taskkill /f do internally?
I read the related question Difference between C# Process.Kill() and Taskkill but it does not have an answer.

Most likely taskkill /f uses TerminateProcess, where as taskkill without /f just posts a WM_QUIT message (not WM_CLOSE). The docs says that TerminateProcess unconditionally kills the process.
You can try following experiments:
Launch notepad.exe and type a few chars in the notpad window
Type taskkill /f /im notepad.exe. Notepad will quit immediately
Now do this:
Launch notepad.exe and type a few chars in the notpad window
Type taskkill /im notepad.exe. Notepad won't quit immediately but it will quit ask if you want to save modifiactions.

Related

How to kill a java application by it's process id from windows cmd?

when i run the command: taskkill /f /pid 16140
I get this : ERROR: The process "16140" not found.
Right click on Name column in Task Manager, check PID to show PID of processes, then execute taskkill /pid {PID}.
Note that some processes cannot be terminated by taskkill, for example Task Manager.
You can kill a process by the process ID (PID) or by image name (EXE filename).
Open up an Administrative level Command Prompt and run tasklist to see all of the running processes:
C:\>tasklist
Image Name PID Session Name Mem Usage
========================= ======== ================ ============
firefox.exe 26356 Console 139,352 K
regedit.exe 24244 Console 9,768 K
cmd.exe 18664 Console 2,380 K
conhost.exe 2528 Console 7,852 K
notepad.exe 17364 Console 7,892 K
notepad.exe 24696 Console 22,028 K
notepad.exe 25304 Console 5,852 K
explorer.exe 2864 Console 72,232 K
In the example above you can see the image name and the PID for each process. If you want to kill the firefox process run:
C:\>Taskkill /IM firefox.exe /F
or
C:\>Taskkill /PID 26356 /F
The /f flag is kills the process forcefully. Failure to use the /F flag will result in nothing happening in some cases. One example is whenever I want to kill the explorer.exe process I have to use the /F flag or else the process just does not terminate.
taskkill /im myprocess.exe /f
The "/f" is for "force". If you know the PID, then you can specify that, as in:
taskkill /pid 1234 /f
Lots of other options are possible, just type taskkill /? for all of them. The "/t" option kills a process and any child processes; that may be useful to you

Windows Command Line wait for task end before next command

I have some software for Windows that opens a website on exit. I want to run a batch that closes the browser after exit from example.exe
example.exe
(wait for end of example.exe)
taskkill /im firefox.exe
taskkill /im iexplore.exe
taskkill /im chrome.exe
The problem you are observing is probably that at the end of your game, the browser is started as another process. It can then take a while before the browser actually starts but the game doesn't wait and just exits. So when you come at the taskkill commands, the browser is still not open. What you need is a way to verify if a browser-process already started, not a way to find out if the exemple.exe (or your game) exited or not. You can do it this way:
:waitOnBrowser
tasklist | findstr /B /C:"chrome.exe" /C:"firefox.exe" /C:"iexplore.exe" > nul
REM the errorlevel will only be 0 if one of the browser has been found in the tasklist
IF ERRORLEVEL 1 goto :waitOnBrowser
REM At this point, you're sure at least one browser has been started
REM taskkill can come here
IF ERRORLEVEL 1 is kind of the same as saying IF %ERRORLEVEL% GTR 1 (see IF in batch).

Batch script to kill a process if not responding in a for loop

I am trying to install some windows standalone update files and to do this I need to use wusu.exe. Every now and again wusu.exe will hang. I have created a batch file called prereqs.bat and in this file I have calls to the wusu.exe. I need the code to kill wusu.exe if it hangs, and retry it again.
This is my code as it stands now:
:PreReqs32
taskkill /im prereqs32.bat /f
taskkill /im wusa.exe /f
when it loops back, it kills both the batch file and wusa.exe
start cmd /k c:\windows\temp\prereqs.bat
An outside process so that I can kill wusu.exe if things go awry.
timeout /t 240 /NOBREAK
this timeout is to wait until the install is complete which is sometimes not enough.
taskkill /im "[wusa.exe]" /fi "STATUS eq NOT RESPONDING"
if "%ERRORLEVEL%"=="1" goto PreReqs32
Is there a way for some sort of FOR loop, with logic to exit if the status is not "not responding"?
Also, as a bonus, is there a way to forgo the timeout and just "wait" for prereqs.bat to be complete before moving on, assuming that wusu.exe has not hung?

How can i close a certain process the user inputted?

I am programming a simple program that will close a process the the user inputs.
For example i will write Chrome , explorer , and then the program will close the chosen process.
My only problem is how do i actually close the chosen process aka p.
Code:
#echo off
#echo Thank you for using my program.
set /p id=Enter Process Name With .exe in the end:
taskkill p /im explorer.exe
timeout 5
%0
You may like to use this line:
taskkill /p /im %id%
or this one which forces the process closed, but it may corrupt data depending on which program is being forced closed.
taskkill /p /f /im %id%

Batch script to close all open Command Prompt windows

I have a .cmd file which I call to open multiple instances of Command Prompt via:
launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd
Each command open a new command prompt.
So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.
Be carefull: you might kill more processes than you want:
taskkill /IM cmd.exe
You can add extra filters:
taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"
use
tasklist /FI "imagename eq cmd.exe " /V
to get a glimpse of what cmd.exe processes will be taskkill-ed
You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.
Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like
start "" my.exe -my -args
where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like
taskkill /IM my.exe
So in some cases it worth to check the real process name, for example in the windows task manager.
TASKKILL /F /IM cmd.exe /T
good solution

Resources