I have a cmd execution takes too long time, I\'d like to set a time out for it. After it\'s time out, then I can go to execute in next line. Is it possible? My cmd is like t
Adapted from a previous answer to handle the cases of killing or keep running the process on timeout
@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