?
Solved

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

Posted on 2010-01-04
15
Medium Priority
?
515 Views
Last Modified: 2012-05-08
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.
0
Comment
Question by:bcrawley01
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26176838
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
0
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 26176850
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
0
 
LVL 57

Expert Comment

by:giltjr
ID: 26176853
Will there only ever be one instance of this program executing?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:bcrawley01
ID: 26176858
yes only one instance
0
 
LVL 57

Expert Comment

by:giltjr
ID: 26176864
How is the batch file being stopped?

From task manager, alt-f4, the "X", cntl-break or any of these?
0
 
LVL 57

Expert Comment

by:giltjr
ID: 26176908
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.
0
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 26176960
We may have a workaround for your ultimate goal. What is the app/task that the .bat is running?
0
 

Author Comment

by:bcrawley01
ID: 26177133
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.

 
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26177142
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
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26177144
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

0
 
LVL 83

Expert Comment

by:leakim971
ID: 26177153
The batch :

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

Open in new window

0
 

Author Comment

by:bcrawley01
ID: 26177192
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?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26177296
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
0
 
LVL 57

Expert Comment

by:giltjr
ID: 26179867
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.

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 26181519
You'll need a secondary script watching your script. The easiest I figured out is to start another batch file monitoring the first cmd.exe. Add a title so we can distinguish it from other command prompts. I setup the secondary script to use input parameters. 1=CMD Title to watch  2=EXE to terminate if still running when CMD Title is no longer running

task.cmd
====================================================
Title "Backup Script"

START "" CheckCMD "Backup Script" "Notepad.exe"

notepad
====================================================


CHECKCMD.CMD
====================================================
@ECHO off
:Loop
ECHO Checking "%~1"
tasklist /v | FIND /i "cmd.exe" | FIND /i "%~1">NUL
IF ERRORLEVEL 1 GOTO SCRIPTENDED
PING -n 10 localhost>NUL
GOTO Loop

:SCRIPTENDED
tasklist | find /i "%~2"
IF ERRORLEVEL 1 GOTO :EOF
taskkill /f /im "%~2" /t
====================================================
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question