我有一个cmd执行需要太长时间,我想为它设置一个时间。在它超时之后,我可以在下一行执行。可能吗?我的cmd是这样的。
example1.exe /e:dosomething.
example2.exe /e:dosomething.
我想为example1.exe设置时间,然后我可以去执行example2.exe 任何方法?
答案 0 :(得分:1)
改编自先前的答案,以处理在超时时杀死或继续运行进程的情况
@echo off
setlocal enableextensions
rem Case 1 - Start example 1 and kill it after 60 seconds
start "" /b example1.exe /e:dosomething
call :timeoutProcess "example1.exe" 60
rem Test why execution continues
if errorlevel 1 (
echo Program has been killed
) else (
echo Program has ended in time
)
rem Case 2 - Start example1 and after 60 seconds continue,
rem leaving example1 running
start "" /b example1.exe /e:dosomething
call :timeoutProcess "example1.exe" 60 1
rem Test why execution continues
if errorlevel 1 (
echo Timeout - Program keeps running
) else (
echo Program has ended in time
)
exit /b
:timeoutProcess process timeout [leave]
rem process = name of process to monitor
rem timeout = timeout in seconds to wait for process to end
rem leave = 1 if process should not be killed
for /l %%t in (1 1 %~2) do (
timeout.exe /t 1 >nul
tasklist | find "%~1" >nul || exit /b 0
)
if not "%~3"=="1" taskkill /f /im "%~1" >nul
exit /b 1