diff --git a/app.py b/app.py index d6b3445..910097d 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,3 @@ -# web_panel/app.py from flask import Flask, render_template, redirect, url_for, request, session import os import subprocess @@ -7,23 +6,33 @@ import psutil app = Flask(__name__) app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key") +# Speichern der Prozess-ID +bot_process = None + def bot_status(): """Überprüft, ob der Bot läuft.""" - for proc in psutil.process_iter(['pid', 'name', 'cmdline']): - if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']: - return True - return False + global bot_process + if bot_process is None: + return False + return bot_process.poll() is None # None bedeutet, dass der Prozess noch läuft def start_bot(): """Startet den Bot.""" - subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__))) + global bot_process + if not bot_status(): + bot_process = subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__))) + else: + print("Bot läuft bereits.") def stop_bot(): """Stoppt den Bot.""" - for proc in psutil.process_iter(['pid', 'name', 'cmdline']): - if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']: - proc.terminate() - break + global bot_process + if bot_process and bot_status(): + bot_process.terminate() + bot_process.wait() # Warten, bis der Prozess beendet ist + bot_process = None + else: + print("Bot läuft nicht.") @app.route("/") def index():