Link to home
Start Free TrialLog in
Avatar of bcrawley01
bcrawley01

asked on

How to kill an exe that has been launched from a batch once the batch has been closed

I have a DOS batch script that calls an EXE. Once this EXE completes the batch continues and exits.

If the batch is running and has launched the EXE then if the batch is killed. The EXE remains open and running. How can I code my batch script so that if the batch is killed it will also kill the running EXE?

Thanks.
Avatar of Bill Prew
Bill Prew

There's no way in a batch script to trap that is is being killed, and take action, so I don't think you will be able to do what you are looking for.  Sorry.

~bp
This might be close to what you are looking for....

start /wait   : Start application and wait for it to terminate

http://ss64.com/nt/start.html
Will there only ever be one instance of this program executing?
Avatar of bcrawley01

ASKER

yes only one instance
How is the batch file being stopped?

From task manager, alt-f4, the "X", cntl-break or any of these?
O.K.  billprew is correct, batch files have no way to know that they have been killed and then execute code after that.

If there  should only be a single instance of the program then you could check to see if the program is already running and then ask the user if they would like it killed.  You can use tasklist and the FOR command to check and see if it is running and taskkill to kill it.

Unfortunately "start /wait" will not work either.  The orignal batch file can still be killed and the started program will continue to run.

Now you might be able to accomplish this using Windows PowerShell:

     http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

PowerShell has a TRAP command that traps errors.  I'm not sure if it would allow you to TRAP a kill and then execute code afterward.
We may have a workaround for your ultimate goal. What is the app/task that the .bat is running?
The .bat stops and starts a few services then runs a .exe that has been provided by our vendor which backup the files for the software provided by them.

The .bat is run as a scheduled task, so sometimes the .exe hangs and after 10 hours I have set it to end the task, so killing the .bat. However when this happens the .exe remains open in which I want it killed with the .bat.

 
It sounds like one approach to this would be to start the EXE from the bat, but don't wait for it to end, just return control right back to the BAT via the START command.  Then the BAT script could go into a loop checking if the EXE ended every minute of 30 seconds.  If it sees it ended it cleans up, but if at 10 mins it is still running it could kill the EXE.  Maybe?

~bp
Hello bcrawley01,

You can launch your exe file from VBScript, ask VBScript to return the Process ID as errorlevel code and use this code in your batch file to kill the process (EXE) :
The VBScript :

(you can remove line 4 and 5)


Set WshShell = CreateObject("WScript.Shell")
cmd = "notepad.exe" 
Set oCmd = WshShell.Exec(cmd)
'WScript.Sleep 3000
'wscript.echo oCmd.ProcessID
wscript.quit(oCmd.ProcessID)

Open in new window

The batch :

(run the VBScript that open the notepad and immediatly close it)
get_processid.vbs
TASKKILL /PID %errorlevel%

Open in new window

How would I then determine within the batch script that the batch script has been killed to execute the TASKKILL /PID %errorlevel% command?

As said previously by others it seems this cannot be done? No way of trapping it and running code on the event of a kill instruction?
If this is a scheduled task, you may run the get_processid.vbs and save the process id to a file (example : echo %errorlevel% > lastpid.txt)
Another scheduled task open the file and use taskkill to kill (or try to kill, case already closed) the process
So after 10 hours you are just blindly killing the batch script?

What I would suggest then is that in the batch script you get the PID of the backup process and write it to a file say "backup.pid".  If the backup process works, the batch script deletes the file "backup.pid".

Then in the process you trigger after ten hours you check for the existence of "backup.pid", if it exists, you read the file, get the PID, and then kill it.

ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

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