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

谁说我不能喝 提交于 2019-12-17 16:27:17

问题


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


回答1:


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



回答2:


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.




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/13515254/how-to-set-a-timeout-for-a-process-under-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!