# -*- coding: utf-8 -*- from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False) def run(cmd: str, timeout: int = 420) -> str: _, out, err = c.exec_command(cmd, timeout=timeout) return (out.read() + err.read()).decode("utf-8", "replace").strip() for i in range(20): st = run("systemctl is-active douyinweb-api") print("api", i, st) if st == "active": break if i == 3: run("systemctl restart douyinweb-api") time.sleep(2) print("unit env:") print( run( "systemctl show douyinweb-api -p EnvironmentFiles -p Environment --no-pager | " "tr -cd '\\11\\12\\15\\40-\\176'" ) ) print("env files:") print( run( "ls -la /etc/systemd/system/douyinweb-api.service /opt/douyinWEB/backend/.env* " "/etc/douyinweb* 2>/dev/null | tr -cd '\\11\\12\\15\\40-\\176'" ) ) print("service snippet:") print( run( "grep -E 'Environment|WorkingDirectory|ExecStart' /etc/systemd/system/douyinweb-api.service " "| tr -cd '\\11\\12\\15\\40-\\176'" ) ) c.close()