import subprocess import os import logging from flask import Flask, request, jsonify import signal import time import psutil logging.basicConfig(filename='osiris_listener_playground.log', level=logging.DEBUG, format='%(asctime)s %(message)s') def read_pid_file(pid_file): """Read the PID from the specified PID file.""" try: with open(pid_file, 'r') as file: pid = int(file.read().strip()) return pid except (FileNotFoundError, ValueError): return None def is_java_process_running(pid): """Check if a Java process with the given PID is running.""" try: proc = psutil.Process(pid) if 'java' in proc.name().lower(): return True except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): return False return False SCRIPTS = { "start_gameservers_minecraft": [ # Vanilla (Port 25565) {"name": "Minecraft Vanilla 1.21", "path": "C:/Users/4lexK/Desktop/GameServers/Minecraft/Vanilla_1-21/start_server.py", "params": ["--java_path", "C:/Program Files/Java/jdk-22/bin", "minecraft_server.1.21"]}, ] } logging.info("Received request to start minecraft servers.") current_pid = [] results = [] for script_info in SCRIPTS['start_gameservers_minecraft']: path = script_info["path"] params = script_info["params"] script_name = script_info["name"] pid_file = os.path.join(os.path.dirname(path), 'server.pid') pid = read_pid_file(pid_file) if pid and is_java_process_running(pid): logging.info(f"Script {script_name} is already running with PID {pid}.") results.append({ "script": path, "params": params, "status": "already running", "pid": pid }) continue try: logging.info(f"Starting script: {' '.join(['python', path] + params)}") process = subprocess.Popen(['python', path] + params, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) psutil_process = psutil.Process(process.pid) logging.info(f"Started {script_name} with PID: {psutil_process.pid}") results.append({ "script": path, "params": params, "pid": psutil_process.pid }) except Exception as e: logging.error(f"Error starting script {path}: {e}") results.append({ "script": path, "params": params, "error": str(e) }) logging.info(f"{results =}") """ current_pid = 8196 try: subprocess.run(['taskkill', '/F', '/PID', str(current_pid)], check=True) print(f"Process with PID {current_pid} has been terminated.") except subprocess.CalledProcessError as e: print(f"Failed to terminate process with PID {current_pid}. Error: {e}") """