3

I'm trying to find out how to limit a program execution time within a Windows batch file. Is there something like Unix timeout command? Please advise.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77

4 Answers4

4

To limit the time a program has to run you could do something like this

start yourprogram.exe
timeout /t 10
taskkill /im yourprogram.exe /f

That starts yourprogram.exe, waits 10 seconds, then kills the program.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • 1
    That's not what Im' looking for. Unix `timeout` command allows to limit execution time of a program/script/command. – Maksym Polshcha Sep 13 '12 at 08:51
  • Ah right, sorry, I have never used Unix before. I have updated my answer, which will hopefully answer your question. – Bali C Sep 13 '12 at 11:56
  • 1
    Actually the program execution can take from 10min to 3-4hours. I'd like to limit this time to 2 hours. So when the app finishes in 10 mins I don't want the batch job to wait 1hr 50 mins. Any suggestions? – Maksym Polshcha Sep 13 '12 at 13:10
  • I'm not sure how you could implement that with batch, unless there was something to check for that program did after it was finished, which the batch could pick up and then end early. Unless you wrote the app in question I can't think of any way to do this. – Bali C Sep 13 '12 at 13:55
2

I just installed Cygwin and use unix-style timeout command from the distribution.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77
0

I don't think there is a timeout command. However, you can start executing a task in the background and sleep (using ping) for the timeout duration then kill the task.

perreal
  • 94,503
  • 21
  • 155
  • 181
0

This code waits 60 seconds, then checks to see if %ProgramName% is running.

To increase this time, change the value of WaitForMinutes.

To decrease the interval between checks, set WaitForSeconds for the number of seconds you want it to wait.

@echo off
set ProgramName=calc.exe
set EndInHours=2

:: How Many Minutes in between each check to see if %ProgramName% is Running
:: To change it to seconds, just set %WaitForSeconds% Manually
set WaitForMinutes=1
set /a WaitForSeconds=%WaitForMinutes%*60

:: How many times to loop
set /a MaxLoop=(%EndInHours%*60*60) / (%WaitForMinutes%*60)

REM Use a VBScript popup window asking to terminate %ProgramName%
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo Wscript.Quit (WshShell.Popup( "Click 'OK' to terminate %ProgramName%." ,10 ,"Terminate %ProgramName%", 0)) >> %tmp%\tmp.vbs

start %ProgramName%
set running=True
:: Give time for %ProgramName% to launch.
timeout /t 5 /nobreak > nul
setlocal enabledelayedexpansion
for /l %%x in (1,1,%MaxLoop%) do (
  if "!running!"=="True" for /l %%y in (1,1,%WaitForMinutes%) do (
    if "!running!"=="True" (
      set running=False
      REM call Pop-Up
      cscript /nologo %tmp%\tmp.vbs
      if !errorlevel!==-1 (
        for /f "skip=3" %%x in ('tasklist /fi "IMAGENAME EQ %ProgramName%"') do set running=True
      ) else (
        taskkill /im %ProgramName%
      )
    )
  )
)
if exist %tmp%\tmp.vbs del %tmp%\tmp.vbs

This code uses VBScript to make a pop-up box. Clicking OK will cause %ProgramName% to be killed via taskkill.


If you do not want to use a pop-up window, you can use timeout by removing...

REM Use a VBScript popup window asking to terminate %ProgramName%
echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo Wscript.Quit (WshShell.Popup( "Click 'OK' to terminate %ProgramName%." ,10 ,"Terminate %ProgramName%", 0)) >> %tmp%\tmp.vbs

...and replacing this...

      REM call Pop-Up
      cscript /nologo %tmp%\tmp.vbs
      if !errorlevel!==-1 (

...with this:

      REM Use CTRL+C to kill %ProgramName%
      timeout /t %WaitForSeconds% /nobreak
      if !errorlevel!==0 (

Using /nobreak is necessary because timeout does not distinguish between pressing a key or timing out. This will allow you to terminate %ProgramName% by pressing CTRL+C , but that causes your batch file to ask Terminate batch job (Y/N)? when you do. Sloppy/Messy/Nasty IMHO.


You could instead use CHOICE by replacing the above mentioned code with this:

      REM Using choice, but choice can get stuck with a wrong keystroke
      Echo [K]ill %ProgramName% or [S]imulate %WaitForSeconds% Seconds
      Choice /n /c sk /t %WaitForSeconds% /d s
      if !errorlevel!==1 (

But choice brings it's own set of limitations to the table. For one thing, it will stop it's countdown if a key that is not among it's choices (in this case s and k) has been pressed, essentially locking up until a correct choice is made. Second, the SPACEBAR cannot be a choice.

James K
  • 4,005
  • 4
  • 20
  • 28