From 62ea52f35da511790abc54ac74446e97b57eb0b4 Mon Sep 17 00:00:00 2001 From: grayhook Date: Sun, 24 Aug 2025 15:23:54 +0700 Subject: [PATCH] check that service already running --- config.ini | 2 ++ main.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/config.ini b/config.ini index 77f4783..60952cc 100644 --- a/config.ini +++ b/config.ini @@ -1,6 +1,7 @@ [minecraft] start_command = service minecraft start stop_command = service minecraft stop +is_running_command = systemctl is-active --quiet minecraft listen_port = 25565 listen_host = 0.0.0.0 check_command = grep "There are [1-9][0-9]* of" <(/var/local/minecraft/mcrcon -H 127.0.0.1 -p password list) @@ -11,6 +12,7 @@ startup_delay_sec = 20 [palworld] start_command = service palworld start stop_command = service palworld stop +is_running_command = systemctl is-active --quiet palworld listen_port = 8211 listen_host = 0.0.0.0 check_command = grep -q '^[1-9][0-9]*$' <(curl -sL -X GET 'http://admin:poopa@192.168.1.21:8212/v1/api/players' -H 'Accept: application/json' | jq -r '.players | length') diff --git a/main.py b/main.py index ce689df..94f1cba 100755 --- a/main.py +++ b/main.py @@ -13,6 +13,25 @@ SHELL_EXECUTABLE = '/bin/bash' # Гарантируем использовани # --- Функции, выполняющие действия --- +def is_service_running(is_running_cmd, service_name): + """ + Проверяет, запущен ли сервис, с помощью предоставленной команды. + Возвращает True, если код возврата 0. + """ + print(f"[{service_name}] Checking if service is already running...", flush=True) + try: + subprocess.run( + is_running_cmd, + shell=True, + check=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + executable=SHELL_EXECUTABLE + ) + return True + except (subprocess.CalledProcessError, FileNotFoundError): + return False + def are_users_there(check_cmd, service_name): """ Выполняет команду проверки через shell. @@ -107,9 +126,16 @@ def run_service_butler(config): Основной цикл конечного автомата для одного сервиса. """ ctx = { "wasted": 0 } - state = "wait_client" # Всегда начинаем с ожидания клиента + service_name = config['service_name'] - print(f"[{config['service_name']}] Butler process started.", flush=True) + print(f"[{service_name}] Butler process started.", flush=True) + + if is_service_running(config['is_running_command'], service_name): + print(f"[{service_name}] Service is already running. Starting in 'check_users' state.", flush=True) + state = "check_users" + else: + print(f"[{service_name}] Service is not running. Starting in 'wait_client' state.", flush=True) + state = "wait_client" while True: state_fn = state_map[state] @@ -143,6 +169,7 @@ def main(): "service_name": service_name, "start_command": config.get(service_name, 'start_command'), "stop_command": config.get(service_name, 'stop_command'), + "is_running_command": config.get(service_name, 'is_running_command'), "listen_port": config.getint(service_name, 'listen_port'), "listen_host": config.get(service_name, 'listen_host'), "check_command": config.get(service_name, 'check_command'),