Page 1 of 1
Trap in a batch file
Posted: 26 Nov 2009 10:59
by susja
Hello,
Please help if you could ..
I have a simple batch file which runs 2 jobs one after another. Sometime I have to interrupt the job because let say something wrong happened during of execution of my application.
In that case I just terminate execution by Control + ^C and answer 'y'. After that I run this command %taskkill.exe ... and kill processes I want to kill. This obviously require additional step after terminating the job.
Here is my goal: I want to use this command %taskkill.exe ... in case I terminate batch file and answer 'y' automatically.
Could someone please help me with it and explain how to include it in my batch and being called in case of Control ^C?
Thanks in advance.
Posted: 26 Nov 2009 11:28
by avery_larry
I think you could do it with 2 batch files. Consider:
test.cmd
Code: Select all
@echo off
echo This is just a control script.
cmd /c test2.cmd
echo This part will run only after test2 finishes OR is killed with ctrl-c
echo So this will also run once test2.cmd finished, which means you
echo might want to put a pause here to allow you to ctrl-c to skip
echo the taskkill
pause
taskkill process
test2.cmd
Code: Select all
@echo off
echo Here's the real code that you want.
Now, if you CTRL-C while test2.cmd is running, then you get the typical "Terminate batch job" prompt, and if you answer "Y", then you'll get a 2nd "Terminate batch job" prompt. The first is for the test2.cmd batch, and the 2nd prompt is for the original test.cmd file. If you now answer "N", then the first test.cmd script will continue to run, getting to your taskkill command.
You're really adding another prompt, though -- because I don't know any way to run the extra commands ONLY if the first one was ctrl-c.
Maybe using start /b /wait
Posted: 26 Nov 2009 11:38
by avery_larry
I've got it. the 2nd batch file will have errorlevel 255 if it's killed with ctrl-c
test.cmd
Code: Select all
@echo off
cmd /c test2.cmd
if %errorlevel%==255 (
taskkill
echo These commands will run if test2.cmd is killed
echo with CTRL-C.
) else (
echo These commands will run if test2.cmd
echo finished on it's own.
)
echo These commands will run whether or not test2.cmd was killed
echo with CTRL-C.
echo All of this is provided, of course, that test.cmd
echo was not ALSO killed.
Posted: 26 Nov 2009 21:54
by susja
hi avery_larry,
First thanks a lot for your reply. Second I love your idea. I thought that DOS has some feature similar to 'trap' on unix when it'll execute on signal but I could use 2 files as well. I tested your files and they work perfect.
The issue is that it doesn't work with my batch file which does the real job
I'll copy/paste my file here.
**
cd "C:\Program files\IBM\IBMIMShared\plugins\com.ibm.rational.test.lt.cmdlineexecute_7.2.0.v200810021504"
start /wait cmd /c cmdline -workspace "C:\CiscoProfile_MC" -project "CiscoWorkloadProj" -schedule "Schedules\SitePrep.testsuite" -eclipsehome "C:\Program Files\IBM\SDP" -plugins "C:\Program Files\IBM\IBMIMShared\plugins"
cmd /c cmdline -workspace "C:\CiscoProfile_MC" -project "CiscoWorkloadProj" -schedule "Schedules\CiscoWorkload_500users.testsuite" -eclipsehome "C:\Program Files\IBM\SDP" -plugins "C:\Program Files\IBM\IBMIMShared\plugins"
cmd /k
**
I replaced the content of test2.cmd with my file. In my case it spawns 2 shells i.e. actual jobs but I don't see the test.cmd any more and only test2.cmd is on the screen and CTRL-C obviously has no expected effect. I assume my issue is related to the content of my batch file. Do you have workaround for it ... would be nice since I feel that I'm very close to resolution with your help.
Thanks again ..
Posted: 27 Nov 2009 13:09
by susja
hi avery_larry,
I figured out how to change my "main" batch file to accommodate your way of solution. So please ignore my previous message.
I do appreciate your help and I learned something new from you.
Regards.