0

I made a batch file (start_loop.bat) with a for loop that runs another batch file (run_program.bat) and kills it after 45 mins. Here is my code:

@echo off
FOR %%A IN (1,1,100) DO (
start run_program.bat
timeout 2700
Taskkill /IM program.exe /F
timeout 3
)
call start_loop.bat

Everything works fine but Taskkill doesn't close the cmd window after killing the program. This is not a big deal but after a while I just end up with a lot of windows open that I don't need. Is there an option to pass to taskkill to close the window?

Also, I had to add

call start_loop.bat

at the end because after couple of loops "start_loop.bat" was ending prematurely. Is it the right way to keep it alive? Is there a better way?

Thank you

Edit after some comments:

1- the loop should run forever

2-it's fine to 'brutally' close the program running

3- the batch file works fine. The program gets killed as expected and a new instance starts as expected. Just the cmd window doesn't close

4- run_program.bat simply runs program.exe with some options e.g.:

program.exe option1 option2
guidout
  • 322
  • 1
  • 4
  • 13
  • By "Taskkill doesn't close the cmd window after killing the program" I suspect you mean the `cmd` window opened by `run_program.bat`. Perhaps if you were to show us the code for `run_program.bat` it would be clearer. – Magoo Jun 02 '18 at 13:27
  • for an endless loop use `for /l %%A in (1,0,2) do ...` (step=0, so the endvalue is never reached) – Stephan Jun 02 '18 at 15:33
  • @guidout For an endless running batch file, replace the __FOR__ command line (second line) by `:Loop` and the last two lines with `)` and `call start_loop.bat` by one line with `goto Loop`. As long as you don't post what `run_program.bat` contains, especially how this batch file starts `program.exe`, it is impossible for us to answer your main question. Perhaps it would be a good idea to run in a command prompt window `start /?` and read output help and read also [How to call a batch file that is one level up from the current directory?](https://stackoverflow.com/a/24725044/3074564) – Mofi Jun 03 '18 at 09:54

1 Answers1

0

Adding exit at the end of the run_program.bat did the trick. Now the window closes once the program gets killed. Here is the code I ended up using and works great for my application:

@echo off
:Loop
start run_program.bat
timeout 2700
Taskkill /IM program.exe /F
timeout 3
goto Loop

Thank you everybody for the suggestions

guidout
  • 322
  • 1
  • 4
  • 13