How to stop a bat process on Windows?

10

5

I have BAT file running in background on Windows which lasts about ten minutes. Now I want to stop it while it's running, but I can't find its name in the process-list in the task manager. So how can I approach this? Thanks!

UPDATE1

It seems difficult to stop a running BAT process in backgroud.And I decide to try to kill every process involved by the BAT file by name,which may be overkilled.It's acceptable to me since most processes in my BAT file are not used frequently,such as ping,tracert,netstat etc.If you have any better solution please let me know.Thanks.

UPDATE2

BAT process tree

alt text

SpawnST

Posted 2010-11-08T06:49:50.257

Reputation: 1 891

1How did you make the bat file run in the background? you say you can't do ctrl-c. i'd try break=on if that doesn't work, but you say you can't do ctrl-c because it is "in the background". Maybe it already ran and finished, and you just have to kill what it spawned. – barlop – 2010-11-17T07:44:34.920

2@barlop,it can be excuted in backgroud with ShellExecute(NULL,"open","some.bat",NULL,NULL,SW_HIDE); in VC++. – SpawnST – 2010-11-17T08:09:23.750

Answers

11

Processes usually get launched in a tree like fashion, I would advise launching Microsoft / Sysinternals Process Explorer, Start by clicking on the Show Process Tree (1) option, then find your process and right click and choose Kill Process Tree (2) This should kill both the original file and everything that it has launched.

alt text

William Hilsum

Posted 2010-11-08T06:49:50.257

Reputation: 111 572

Killing the Process Tree seems a little heavy handed. It may be sufficient to kill the parent process (the bat command). – wag2639 – 2010-11-17T08:39:01.143

1@wag2639, after reading the question, I thought he wants to end the BAT and spawned processes? When it is said that it takes so long to process, I understand that as processes get launched and then nothing happens until they write a response... If the tree isn't ended, there will just be a bunch of running processes that are doing nothing. – William Hilsum – 2010-11-17T08:41:19.410

I've tried this way and added the picture in my answer.Thanks.And now I find a command that can kill a process started by BAT file and its children taskkill /F /T /IM cmd.exe.The only problem is ,every BAT file will start a cmd.exe process with diffrent PID.I need to figure out which one is my target. – SpawnST – 2010-11-17T09:20:48.880

@SpawnST, what process is launching the BAT file? You need to kill the item that is launching it then if there are multiple copies. – William Hilsum – 2010-11-17T10:03:44.580

@Wil,it's started by a MFC/Dialog process.The dialog only run the BAT file with window shell when it's initialized.Now I'm trying to ensure that the BAT process is teminated completely when the dialog exits. – SpawnST – 2010-11-17T10:22:19.163

How do you find the process for a given .bat file? – Peter Nore – 2011-06-13T11:38:00.543

@peter nore - if you have multiples, you can use the last icon on the toolbar (the target button) and drag it to the open bat file. – William Hilsum – 2011-06-13T11:44:50.707

@WilliamHilsum - thanks for Process Explorer! It is a great tool. – Bulwersator – 2014-05-13T11:04:33.957

5

You can modify the batch file so that it uses a lock-file to continue operating, by inserting checks between commands for the existence of the file. To stop the batch file from executing, just delete the lock-file.

Here is a demo batch file:

echo xx > "c:\temp\lockfile"
pause
if not exist "c:\temp\lockfile" goto  exit
pause
del "c:\temp\lockfile"
:exit

To violently kill the processes that might be executing at the moment, you can create a 'kill' batch file that will contain taskkill commands for all the tasks possibly launched from the batch file:

del "c:\temp\lockfile"
taskkill /im mytask1.exe
taskkill /im mytask2.exe

harrymc

Posted 2010-11-08T06:49:50.257

Reputation: 306 093

That's a good idea.But I think it may cause some problem to my app when a command executes for a long time,tracert for example. – SpawnST – 2010-11-17T09:33:24.490

yeah,that's what i'm doing now ,really violent one indeed:-).And perhaps the lock file is not necessary in this solution. – SpawnST – 2010-11-17T10:41:32.913

It is necessary so the batch file doesn't execute the next task when its current one is terminated. It could otherwise happen that task1 is terminated and task2 is started after its taskkill was already done, so the kill batch will simply miss up on task2. – harrymc – 2010-11-17T11:27:32.080

1

A BAT typically just launches an instance of CMD.exe. Depending on what your script does, it will also start other processes.

vcsjones

Posted 2010-11-08T06:49:50.257

Reputation: 2 433

So is there any way that can stop it keeping starting other processes? – SpawnST – 2010-11-08T07:24:53.243

1@SpawnST: If the CMD window is visible, can't you just type CTRL+C? – paradroid – 2010-11-08T11:40:34.430

@jason404,as i mentioned in the question,it's running in background. – SpawnST – 2010-11-09T07:33:02.573

1

I wrote an app that might help you, at least if the BAT is running in a CMD window. You can view the application windows that are running and get the PID of the app, you can then kill it with my app, you can also see all the processes that it has spawned and kill them too. Its a real simple program, but effective for this sort of thing. You can also use it to track down exactly where on your HD the BAT file is running from, in case you didn't know that already.

MaQleod

Posted 2010-11-08T06:49:50.257

Reputation: 12 560

I'm doing this work with VC++/MFC,and I think I need some windows commands to get it done.thanks for the response. – SpawnST – 2010-11-08T07:42:40.280

1

How is the batch file invoked?
If it is from the Task Scheduler, then you can stop it from there as well. If it is from a service, then you can stop the service.

weismat

Posted 2010-11-08T06:49:50.257

Reputation: 335

1

Found this message board thread:

http://www.gamedev.net/community/forums/topic.asp?topic_id=261694

It suggests using ShellExecuteEx (or CreateProcess)so you can get a process handle, that then can be used to kill the process.

Chris Hulan

Posted 2010-11-08T06:49:50.257

Reputation: 116

0

If you have it open in command prompt you can press Ctrl+C. Wait a few seconds and it will be terminated.

harvey hopkins

Posted 2010-11-08T06:49:50.257

Reputation: 1