Search code examples
batch-filecmdtimer

timer for closing a program in cmd


I want to make a bat file that starts a program(actually another bat) and close the program after a certain amount of time.

my program is like this.

-In 'Program_A.bat'

@echo off
set /p sec="Enter the amount of sec you want to run : "

start Program_B.bat

timeout /t %sec%

tasakkill /F /IM cmd.exe

I found It works well when I put little amount of time. like 10 seconds. But When I put 3600 or more, It doesn't kill the Program_B.bat properly.

And I if I put 172800 secs in order to set 48 hours, it doesn't work.

How can I manage these problems?


Solution

  • killing just cmd.exe may kill windows, that you don't want to kill.
    You can give your application a unique title and kill it by that title.
    Also to work around the 100000 seconds limit of timeout, just timeout one hour in a loop:

    start  "UniqueTitle" Program_B.bat
    for /l %%a in (1,1,48) do timeout 3600 >nul
    taskkill /fi "windowtitle eq UniqueTitle"