Link to home
Start Free TrialLog in
Avatar of carbonbase
carbonbaseFlag for United Kingdom of Great Britain and Northern Ireland

asked on

how do i kill an exe spawned by a batch file?

Hi, I have a batch file which runs a robocopy command.  I am using Windows task scheduler to run the scrpt nightly.  I have schueduled the batch file to run between 8pm and 8am when the users aren't around so that most files robocopy tries to copy are not in use.  

The problem I am having is that if task scheduler terminates the batch file at 8am the robocopy process is still left running.  Is there a way I can add to the batch file so that when the batch file closes the robocopy process it has spawned is also terminated?  

My batch file is below:

@echo off

set src="U:\source folder"
set dest="V:\destination folder"
set logname="C:\temp\Robocopy_Mirror.txt"

robocopy %src% %dest% /TBD /MT:32 /MIR /XJ /XF *.tmp /XD "$RECYCLE.BIN" "System Volume Information" /ZB /R:1 /W:1 /LOG+:%logname%

Open in new window



To make things more complicated I am running other robocopy jobs on the same computer so ideally I want to kill only the process spawned by this particular batch file.
Avatar of Qlemo
Qlemo
Flag of Germany image

No, you do not have any way to force-close if the batch file is killed. In a batch you also don't know the PIDs of "child" processes. But you can use a particular user for the task, and kill all processes of that user at 8:00am.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of carbonbase

ASKER

Your solution seems to work well,  I had a slight issue because I had to map the drives first before the robocopy command, but I solved that by adding an action to map the drives before running the robocopy command.  I tested running multiple robocopy jobs on the same computer and only the robocopy process spawned by the schedule task is killed when I terminate the task.  

Thanks very much!
Avatar of Bill Prew
Bill Prew

Welcome, glad that helped...

~bp