替换server_configurations 中得配置信息即可!
已经自用两个月,目前来看服务都能被正常拉起来!
只需要找一个服务器或者电脑用python运行这段脚本即可!
import paramiko
import time
import schedule # 导入 schedule 库
def check_service_and_start(hostname, port, username, password, service_name, service_process_name,
service_start_command):
"""
连接到远程服务器,检查指定服务是否运行,未运行则启动。 (函数代码保持不变)
"""
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print(f"尝试连接到 {hostname}:{port} 以检查服务: {service_name} ...")
client.connect(hostname, port=port, username=username, password=password, timeout=10)
print(f"成功连接到 {hostname}:{port}")
# 检查服务是否正在运行 (检查服务状态的代码保持不变)
check_command = f"pgrep {service_process_name}" # 注意: pgrep 在 Windows 上不可用,需要替换
print(f"执行命令检查 {service_name} 服务状态: {check_command}")
stdin, stdout, stderr = client.exec_command(check_command)
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
print(f"{service_name} 服务正在运行。")
else:
print(f"{service_name} 服务未运行,尝试启动...")
start_command = service_start_command
print(f"执行启动命令: {start_command}")
stdin, stdout, stderr = client.exec_command(start_command)
start_output = stdout.read().decode()
start_error = stderr.read().decode()
if start_output:
print("启动命令输出:")
print(start_output)
if start_error:
print("启动命令错误:")
print(start_error)
# 再次检查启动后服务是否运行 (可选,可以增加等待时间)
time.sleep(2)
stdin, stdout, stderr = client.exec_command(check_command)
exit_status_after_start = stdout.channel.recv_exit_status()
if exit_status_after_start == 0:
print(f"{service_name} 服务已成功启动。")
else:
print(f"{service_name} 服务启动失败或仍然未运行,请检查启动命令的输出和错误信息。")
except paramiko.AuthenticationException:
print("SSH 认证失败,请检查用户名和密码是否正确。")
except paramiko.SSHException as e:
print(f"SSH 连接错误: {e}")
except Exception as e:
print(f"发生未知错误: {e}")
finally:
if client:
client.close()
print("SSH 连接已关闭。\n")
def run_server_checks():
server_configurations = [
{
"hostname": "web15.serv00.com",
"port": 22,
"username": "xxxxx",
"password": "xxxxx",
"service_name": "Nezha Agent",
"service_process_name": "nezha-agent",
"service_start_command": "/home/fdageag/nezha_app/agent/nezha-agent.sh >/dev/null 2>&1"
},
]
for server_config in server_configurations:
check_service_and_start(
hostname=server_config["hostname"],
port=server_config["port"],
username=server_config["username"],
password=server_config["password"],
service_name=server_config["service_name"],
service_process_name=server_config["service_process_name"],
service_start_command=server_config["service_start_command"]
)
print("所有服务器的服务检查完成。(本次运行)")
if __name__ == "__main__":
print("定时任务脚本开始运行...")
# 首次立即运行一次服务检查
run_server_checks()
# 使用 schedule 库设置每 3 小时执行 run_server_checks 函数
schedule.every(3).hours.do(run_server_checks)
while True:
schedule.run_pending() # 检查是否有待执行的任务并执行
time.sleep(60) # 每60秒检查一次,减少 CPU 占用
