check that service already running
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user