How to stop process from .BAT file?

158,741

Solution 1

To terminate a process you know the name of, try:

taskkill /IM notepad.exe

This will ask it to close, but it may refuse, offer to "save changes", etc. If you want to forcibly kill it, try:

taskkill /F /IM notepad.exe

Solution 2

As TASKKILL might be unavailable on some Home/basic editions of windows here some alternatives:

TSKILL processName

or

TSKILL PID

Have on mind that processName should not have the .exe suffix and is limited to 18 characters.

Another option is WMIC :

wmic Path win32_process Where "Caption Like 'MyProcess.exe'" Call Terminate

wmic offer even more flexibility than taskkill .With wmic Path win32_process get you can see the available fileds you can filter.

Solution 3

When you start a process from a batch file, it starts as a separate process with no hint towards the batch file that started it (since this would have finished running in the meantime, things like the parent process ID won't help you).

If you know the process name, and it is unique among all running processes, you can use taskkill, like @IVlad suggests in a comment.

If it is not unique, you might want to look into jobs. These terminate all spawned child processes when they are terminated.

Solution 4

I just wanted to kill all instances of Chrome when the browser won't open (recent Chrome annoying bug). You can end up with a bunch of chrome.exe processes running from 2 to ?. I just did a clean and quick check to see if it's running and kill it if it is. The pause at the end is only so I can view the results, it isn't needed.

@echo off
:again
taskkill /F /IM "chrome.exe"
if errorlevel=0 goto end
if errorlevel=1 goto again
:end
pause

it works well for me

Solution 5

Edit: call runntaskkill.bat is changed to call taskkillapp.bat or else it will end up with the same file.

You can make two batch files. One named runtaskkill.bat and another named taskkillapp.bat. To the file runtaskkill.bat add the following code:

call taskkillapp.bat [filenamehere].

And to taskkill.bat:

taskkill /f /im %1.

Just make sure that the two files are in the same directory (folder).

Share:
158,741
Rella
Author by

Rella

Hi! sorry - I am C/C++ noobe, and I am reading a book=)

Updated on February 10, 2022

Comments

  • Rella
    Rella over 2 years

    So I have process I started from one bat file. How to stop it from another?

  • Henno Vermeulen
    Henno Vermeulen almost 9 years
    Works like a charm. Indeed the option without /F asks the program to close nicely. The program can then still decide to not close at all. I have a program that minimizes to a tray icon when the close button is pressed and that is exactly what happens. The /F option forcibly exits it.
  • Wolfpack'08
    Wolfpack'08 over 8 years
    taskkill /F /IM notepad.exe better in most cases for me. taskkill /F /IM notepad.exe throws errors at me from tasks that don't have save dialogs. They don't close properly and different things can happen (the program may stay open, even after the dialog is closed). I wouldn't want an error to be generated in the background, either, so I recommend /F /IM unless it's Word.exe or something.
  • Iogui
    Iogui almost 3 years
    This answer is not adding noting new that wasn't mentioned on another answers and also it is confuse because you've just added your script made to solve a different problem then the mentioned by the question.