Skip to content Skip to sidebar Skip to footer

Batch That Monitors And Restarts A Python

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 te

Solution 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 change call command as follows:

CALL command now accepts labels as the target of the CALL. The syntax is:

CALL :label arguments

  • I specify the window title with the start command, so my code will work. Type start /? in a fresh cmd window.

  • I call the infinite 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 format window title - filename. Even if it doesn't exist tasklist will return errorlevel value 0 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 is 1, 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 called the infinite subroutine, after its end, we will return to check_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:

  1. 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.

  1. Some interesting references:

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.

Post a Comment for "Batch That Monitors And Restarts A Python"