[Python] Main script that start, stop and monitor others

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    Original ask question:

    I have a watchdog.py this script should handle the other scripts let's say test1.py and test2.py

    script1.py and script2.py run on their own in loop as let's say closed modules.

    The only Task of main.py is to start, stop the other if needed and restart them if the run into an error (monitor).

    Have looked at popen and multi threading but I found no way to monitor and restart.

    Can someone lead me a way to the right procedure or give me an example how do to that?

    My solution so far that works (On Linux):

    In the main i call every script with a popen subprocess and kill it with pkill. From my point of view its not the best solution what brings me back to my main question for the right procedure...

    watchdog.py:

    Main Script that runs in a infinite loop and start/stop/restart scripts if necessary

    whatchdog.ini:

    The config file where the scripts and start/stop/ restart flags are defined. It can by altered manually or by another script while whatchdog.py runs.

    test1.py:

    A simple test script that prints "Hello Test 1" every 10sec

    test2.py:

    A simple test script that prints "Hello Test 1" every 10sec but run in an error after 2x (will trigger an restart if flag in the ini is 1)

    Put everything in an Folder and run watchdog.py its a test script the only thing is missing is the possibility to set a full file path for the scripts but that is easy to change if you want to use it ...

    Code watchdog.py:

    import time
    import os
    import subprocess
    from multiprocessing import Process
    import configparser

    #Time interval how often should the watchdog script run (sec.)
    interval = 10

    # // FUNCTIONS //

    # Function to read the config file

    def read_config(section, key):
    config = configparser.ConfigParser()
    config.read('watchdog.ini')
    return config[section][key]


    # Function to start the Script as subprocess inside multiprocessing process

    def start_script(scriptname):
    sub_dict = {}
    sub_id = "sub_id_" + scriptname
    sub_variable_name = "start_" + scriptname
    sub_variable_value = ['python', '' + str(scriptname) + '.py']
    sub_dict[sub_variable_name] = sub_variable_value
    sub_dict[sub_id] = sub_id
    globals()[sub_dict[sub_id]] = subprocess.Popen(sub_dict[sub_variable_name])

    # Function to kill (stop) a subprocess/script

    def stop_script(scriptname):
    command = 'pkill -f ' + str(scriptname) + '.py'
    os.system(command);

    # Check if subprocess/script is running
    def running_script(scriptname):
    sub_id = 'sub_id_' + scriptname
    if sub_id in locals() or sub_id in globals():
    if eval(sub_id).poll() == None:
    #print(scriptname + ' is running')
    return('running')
    else:
    #print(scriptname + ' not running')
    return('stoped')
    else:
    return('not defined')

    # // END FUNCTIONS //

    # // MAIN LOOP //

    while True:
    # Loop throug sections of the config file to get all scripts
    config = configparser.ConfigParser()
    config.read('watchdog.ini')
    all_section_names: list[str] = config.sections()
    for section_name in all_section_names:
    #print(section_name)
    #Get start/stop/restart flags for the specific script
    start = int(read_config(section_name, 'start'))
    stop = int(read_config(section_name, 'stop'))
    restart = int(read_config(section_name, 'restart'))
    #Check Status running / stoped / not defined
    status = str(running_script(section_name))
    #Start script if start=1 and wasnt started before
    if start == 1 and status == 'not defined':
    start_script(section_name)
    print(section_name + ' started')
    # Stop the script if stop = 1
    if stop == 1 and status != 'stoped' and status != 'not defined':
    stop_script(section_name)
    print(section_name + ' stoped')
    # Restart script if restart = 1 and script was started before
    if restart == 1 and status == 'stoped':
    stop_script(section_name)
    start_script(section_name)
    print(section_name + ' restarted')
    time.sleep(interval)


    # // END MAIN LOOP //


    Code watchdog.ini:

    [test1]
    start = 1
    stop = 0
    restart = 0

    [test2]
    start = 1
    stop = 0
    restart = 1


    Code test1.py

    # Scripts prints 'Hello Test 1' every 10sec and run without problems
    import time

    while True:
    print('Hello Test 1')
    time.sleep(10)


    Code test2.py:

    # Scripts prints 'Hello Test2" every 10sec but after two times it runs into an error and need to be / could be restarted by the main.py
    import time

    while True:
    print("Hello Test 2")
    time.sleep(10)
    print("Hello Test 2")
    time.sleep(10)
    get.value

    Continue reading...

Compartilhe esta Página