A
A
alex_devPY2020-08-21 05:22:27
cmd/bat
alex_devPY, 2020-08-21 05:22:27

How to set timeout?

Hello.
There is a process.bat

@ECHO OFF
start /B python  script1.py
start /B python  script2.py
:LOOP
tasklist | find /i "python.exe" >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  ECHO script is still running
  Timeout /T 5 /Nobreak
  GOTO LOOP
)
:CONTINUE
python script3.py


I run script1 and script2 at the same time, wait for them to complete, and execute script3.

How can I set a separate timeout for script1 and script2 in bat?

If the script runs for more than N seconds, the process is deleted.

Something like
start /B python script2.py /t 600 exit

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2020-08-21
@alex_devPY

that's strange, you have python scripts, in python there are many ways to organize a controlled subscript execution timeout, but you want cmd \ bat

import subprocess
try:
    r = subprocess.run(['python', 'script2'], timeout=5)
except subprocess.TimeoutExpired as e:
    print(e)
try:
    r = subprocess.run(['python', 'script1'], timeout=50)
except subprocess.TimeoutExpired as e:
    print(e)

That's strange, you have windows, almost any windows has powershell, which is great at managing processes, but you want cmd \ bat
$p1=start 'python  script1.py' -PassThru -NoNewWindow
$p2=start 'python  script2.py' -PassThru -NoNewWindow
$p1 | Wait-Process -Timeout 10 -ErrorAction SilentlyContinue -ErrorVariable t1;if ($t2) { $p1 | kill}
$p2 | Wait-Process -Timeout 500 -ErrorAction SilentlyContinue -ErrorVariable t2;if ($t2) { $p2 | kill}

Maybe we should stop debauchery and bury the stewardess?

S
sHARek, 2020-08-21
@sHARek

win+r => cmd => timeout /? - ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question