Search code examples
batch-filecmdtaskkill

taskkill close windows and for loop ending prematurely


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

Solution

  • 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