0

i am trying to write a watchdog that will monitor the changes in Main.py file and will restart the script if the error occures. The watchdog works on files that doesn't include RPi.GPIO library, but when I add it, it can't find it. Can someone help me figure it out ?

This is the watchdog:

import sys

import subprocess
import psutil
import time


while True:
    # Check if Main.py is already running
    processes = [process for process in psutil.process_iter() if process.name() == 'python' and 'Main2.py' in process.cmdline()]

    if len(processes) == 0:
        # Process is not running, start it
        print('Process is not running, starting it now...')
        # Add your code to start the process here
        subprocess.Popen(['python', 'Main2.py'])

    # Check if all required modules are imported
    try:
        import Ultrasonicky_senzor as us
        import Nadrz
        import Signalizacia
        import Globalne_premenne as gp
    except Exception as e:
        print("Chyba pri načítaní súborov: ", e)
        # Kill the process and start it again
        for process in processes:
            process.kill()
        time.sleep(10)
        continue

    print("Všetky súbory úspešne načítané")
    time.sleep(10)

Watchdog fix For GPIOs

0 Answers0