#!/usr/bin/env python2
import os
import time
import psutil # Import the psutil library
LOG_FILE = "/home/TestServer/restart_log.txt"
def check_and_restart(game_server_name, executable_path):
if not any(p.info['name'] == game_server_name for p in psutil.process_iter(attrs=['pid', 'name'])):
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
with open(LOG_FILE, "a") as log_file:
log_file.write("{0} - {1} is down. Restarting...\n".format(current_time, game_server_name))
os.chdir(executable_path)
os.system("./{0} &".format(game_server_name))
# Example usage
while True:
check_and_restart("TestGameServer_d", "/home/TestServer/GameServer1/gameserver_1")
check_and_restart("TestGameServer_d2", "/home/TestServer/GameServer2/gameserver_1")
time.sleep(15)