40

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

Rella
  • 65,003
  • 109
  • 363
  • 636
  • 2
    `taskkill -im exename.exe` will forcefully terminate the process. Is this what you want? – IVlad May 22 '10 at 16:31
  • 2
    Possible duplicate of [Any way to write a Windows .bat file to kill processes?](http://stackoverflow.com/questions/33822/any-way-to-write-a-windows-bat-file-to-kill-processes) – Alex Kulinkovich Jun 23 '16 at 16:06

9 Answers9

54

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
fmark
  • 57,259
  • 27
  • 100
  • 107
  • 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. – Henno Vermeulen Jul 29 '15 at 08:37
  • 1
    `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. – Wolfpack'08 Dec 15 '15 at 05:14
5

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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
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

Luap
  • 41
  • 1
  • 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. – Iogui Aug 27 '21 at 16:40
2

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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

taskkill /F /IM notepad.exe this is the best way to kill the task from task manager.

1

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).

0

Here is how to kill one or more processes from a .bat file.

Step 1. Open a preferred text editor and create a new file.

step 2. To kill one process use the 'taskkill' command, with the '/im' parameter that specifies the image name of the process to be terminated. Example:

taskkill /im examplename.exe

To 'force' kill a process, use the '/f' parameter which specifies that processes be forcefully terminated. Example:

taskkill /f /im somecorporateprocess.exe

To kill more than one process you repeat the first part of step 2 with the appropriate processes for the process name. Example:

taskkill /im examplename.exe
taskkill /im examplename1.exe
taskkill /im examplename2.exe

or

taskkill /f /im examplename.exe
taskkill /f /im examplename1.exe
taskkill /f /im examplename2.exe

Step 3. Save your file to your desired location with the .bat extension.

Step 4. Click the newly created bat file to run it.

DJN
  • 153
  • 2
  • 10
0

To just click file and run with no message "press any key to continue"

@echo off
call taskkill /f /im adb.exe /im OfficeClickToRun.exe
pause>nul
exit /b 0

add any amount of processes:

/im the_process_name.exe

TIP:

Create a shortcut of the .bat file somewhere (you can drag it to start menu/all programs) , go to the shortcut file properties and add a shortcut key

trinib
  • 119
  • 1
  • 3
-1

Why don't you use PowerShell?

Stop-Process -Name notepad

And if you are in a batch file:

powershell -Command "Stop-Process -Name notepad"
powershell -Command "Stop-Process -Id 4232"
Rosberg Linhares
  • 3,537
  • 1
  • 32
  • 35