Edit: I have updated my question and code.
I am running a python script (on hundreds of files) from a batch file. Problem is, python script on some files takes a lot of time to run. What I want is that my python script should not run for more than 5 minutes on a single file. If it takes more than 5 minutes, I would simply want to kill my running script and move to the next file as written in my batch file code below.
What I want is, to define 300 seconds as the maximum time my code can spend on one file. If it takes more than 300 sec, batch file should kill it. However, when it takes less than 300 sec, it should not wait for 300 sec, rather should move to the next statement immediately.
@echo off
setlocal enableextensions disabledelayedexpansion
echo FILE NO: 1
rem filename1 is not a variable name but the name of the file itself
start "Python" python "Code.py" filename1 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename1 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 2
start "Python" python "Code.py" filename2 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename2 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 3
start "Python" python "Code.py" filename3 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename3 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 4
start "Python" python "Code.py" filename4 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename4 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 5
start "Python" python "Code.py" filename5 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename5 var2 & call :timeoutProcess "python.exe" 300
.
.
.
.
.
.
.
echo FILE NO: 200
start "Python" python "Code.py" filename200 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename200 var2 & call :timeoutProcess "python.exe" 300
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 on timeout
for /l %%t in (1 1 %~2) do (
timeout /t 1 >nul
tasklist | find /i "%~1" >nul || exit /b 0
)
if not "%~3"=="1" taskkill /f /im "%~1" >nul
exit /b 1
This code perfectly works fine if I run it till filename5. However, when I ran it upto filename200, this code only runs for filename1 and straightaway moves to filename13 and then filename170. I am not able to understand this issue. There does not seem to be any syntax error.
I am new to writing batch files. Kindly help in solving this issue. If you have any better alternative to solve this problem (instead of timeout), then please do let me know.
P.S.: I also tried to use Timeout function in Python from Eventlet package but this function does not work properly on my files and therefore I thought I should try timeout from batch file instead. I am working on Windows machine and using Python 2.7.