Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to start a program with a windows batch file. But the program should stop after a certain timeout value. For example: Run the program 60 seconds and stop it after 60 seconds.

Under Linux, there is this nice timeout command to do what I want. Windows has also a timeout command, but its just to pause a command, to delay its execution. Is there something else under Windows to do so ?

Setup: Windows 7, 64 Bit, Professional

like image 269
John Threepwood Avatar asked Nov 22 '12 14:11

John Threepwood


People also ask

How do you pause 10 seconds in a batch file?

You can insert the pause command before a section of the batch file that you might not want to process. When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.

What is Windows timeout command?

Pauses the command processor for the specified number of seconds. This command is typically used in batch files.

How do I set a batch timeout?

Timeout with the parameter /NOBREAK If we take the example from before and run that in a BATCH file: timeout /t 60 then while waiting those 60 seconds, you are actually able to break the timeout by pressing any key on your keyboard. To prevent this we simply add the parameter /NOBREAK to the end of it.

What is timeout EXE?

timeout.exe can be used in a cmd.exe batch file or a PowerShell script to pause execution («sleep») for a specified amount of seconds.


2 Answers

start yourprogram.exe timeout /t 60 taskkill /im yourprogram.exe /f 
like image 97
Bali C Avatar answered Oct 02 '22 03:10

Bali C


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.

like image 40
LMercury Avatar answered Oct 02 '22 05:10

LMercury