问题
I'm trying to make a batch that restarts my python files whenever any of them close or crash, with the following command:
@echo off
:a
cd C:\Users\PC\Desktop\testfolder
test1.py
test2.py
test2.py
goto a
but it is not working, what should I do?
回答1:
A combination of an "infinite loop" which is needed in your case and python files will overload your CPU a lot I think. Have a revised piece of code (working only in single file extensions (*.bat, *.txt
)). See below for something more general.
@echo off
setlocal EnableExtensions
:start_python_files
start "1st" "test1.py"
start "2nd" "test2.py"
start "3rd" "test3.py"
:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files
:infinite
tasklist /FI "WINDOWTITLE eq %1 - %2" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "%2")
Well, this way may last some time, so if a file is closed (~2-3 secs depending on your CPU overload).
Please notify me if it is not working for you. I haven't python installed and I don't know how they are named when they are opened :).
So, now as you have (kindly???) requested complete answers let me explain my code:
- I enable extensions (
setlocal EnableExtensions
) to changecall
command as follows:
CALL command now accepts labels as the target of the CALL. The syntax is:
CALL :label arguments
From call /?
command. You should type it in a fresh cmd for more information
I specify the window title with the
start
command, so my code will work. Typestart /?
in a fresh cmd window.I
call
theinfinite
subroutine sending to it arguments (window title and filename). These can be accessed with%1
(first argument) and%2
(second argument).In the
infinite
subroutine, I search for window title (WINDOWTITLE
) EQUAL (eq
) to formatwindow title - filename
. Even if it doesn't existtasklist
will returnerrorlevel
value0
with the message:
INFO: No tasks are running which match the specified criteria.
As here PID
string doesn't exist (if it is found it will exist), we put findstr
to search for it. If found, errorlevel
will be 0
. Else, it would be 1
.
If the
errorlevel
is1
, that means that process not found, which means that the file is closed. So, we reopen it with the arguments sent (start "window title (%1)" "filename (%2)"
).As we have
call
ed theinfinite
subroutine, after its end, we will return tocheck_python_files
subroutine doing all of these above infinitely, until user termination or computer shutdown.
As later discussed in chat, when we run python files standardly (with start "window title"
) window title will be the full path to python.exe
file. I found a way to fix it: start
the cmd /c
command. A revised piece of code:
@echo off
setlocal EnableExtensions
:start_python_files
start "1st" "cmd /c test1.py"
start "2nd" "cmd /c test2.py"
start "3rd" "cmd /c test3.py"
:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files
:infinite
tasklist /FI "WINDOWTITLE eq %1" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "cmd /c %2")
I have just added only a cmd /c
extra (and remove %2
) from window title as it was not needed.
cmd /c
tells system to run a new cmd which will carry out the command specified by string and then it will terminate.
Synopsis:
Commands should be run to get more information about how they work:
call /?
start /?
goto /?
tasklist /?
findstr /?
cmd /?
I suggest to run the above in a fresh new cmd window.
Some interesting references:
- https://ss64.com/nt/start.html
- How do I pass command line parameters to a batch file?
- Set Windows command-line terminal title in Python
I am really sorry for putting you into this mess. In anyway, thank you for providing such good information for me to understand where I was wrong.
来源:https://stackoverflow.com/questions/53806291/batch-that-monitors-and-restarts-a-python