How to set a timeout for a process under Windows 7?

Bali C gave a concise and to the point answer. I needed something a little more featureful and reusable. Based on Bali C's Example. I came up with this. If anyone should need the same as me.

your.bat

REM...

CALL STARTwaitKILL..bat /relative/path/your_program.exe

REM...

STARTwaitKILL.BAT

@ECHO OFF 
IF[%1]==[] GOTO EOF
IF NOT EXIST %1 GOTO EOF

REM SET PRIORITY=/NORMAL
REM ADJUST PRIORITY, BELOWNORMAL LETS BATCH FILE RUN MORE SMOOTHLY
REM WITH A PROGRAM THAT CONSUMES MORE CPU. SEE ABOUT MAXWAIT BELLOW
SET PRIORITY=/BELOWNORMAL
REM SET PRIORITY=/LOW
REM 0 NORMAL WINDOW :: 1 NO WINDOW :: 2 MINIMIZED WINDOW
SET /A HIDDEN=1
REM MAXWAIT HERE IS MORE LIKE MINIMUM WAIT IN WINDOWS.
SET MAXWAIT=10
SET WAITCOUNT=0

SET ARGS=/I %PRIORITY%
IF %HIDDEN% EQU 1 SET ARGS=%ARGS% /B
IF %HIDDEN% EQU 2 SET ARGS=%ARGS% /MIN

START %ARGS% %1

:WAIT
IF %WAITCOUNT% GEQ %MAXWAIT% GOTO KILL_IT

TIMEOUT /T 1 > NUL
SET /A WAITCOUNT+=1
FOR /F "delims=" %%a IN ('TASKLIST ^| FIND /C "%~nx1"') DO IF %%a EQU 0 GOTO RUN_DONE
GOTO WAIT

:KILL_IT
TASKKILL /IM %~nx1 /F > NUL
:RUN_DONE

Could be fleshed out ore to take more arguments for priority and such, but I don't have the need for it. Shouldn't be hard to add.


Don't exist any command in Windows to delay an app or to set a timeout for an app

Timeout in Windows is for Delay the execution process of CMD/Batfile, nothing more utility.

You can use external tools for that, I don't remember the name of any now, so many underground software, sorry, but I remember that in the autoit official forum exists a similar commandline tool to launch an app setting the timeout, and maybe in the tool NIRCMD, or ps2exec, check their help files, or someone inside the WAIK Kits.

This is the only you can do:

@Echo OFF

:: First launch the app in background mode, because some applications stops the execution of CMD.
Start /B ".\Dir\Your app.exe"

:: Then stay in background for a certain time
Timeout /T "Seconds"

:: Continue your code...
Pause&Exit

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

The start+timeout+taskkill waits exactly the given time. Since I needed to stop waiting if the process exits earlier, I created my own solution in C++.

The tuxliketimeout program mimics the GNU timeout. Feel free to download&compile from

https://github.com/cernoch/tuxliketimeout