# -*- coding: utf-8 -*- """Kill orphaned Next build processes on dy and clean restart web.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") PASS = "Xiao4669805" REMOTE = r''' set +e echo "=== before ===" uptime ps -eo pid,pcpu,etime,cmd --sort=-pcpu | head -8 echo "=== kill orphans ===" # Next SWC/native build leftovers (exe name "usr" under deleted .r* dirs) pkill -9 -f '/opt/douyinWEB/frontend/\.r.*/usr' 2>/dev/null || true # Orphan bare "usr" binary for p in $(pgrep -x usr || true); do exe=$(readlink -f /proc/$p/exe 2>/dev/null || true) echo "usr pid=$p exe=$exe" case "$exe" in *douyinWEB/frontend*|*deleted*) kill -9 $p; echo killed_usr_$p ;; esac done # Orphan node with cmdline exactly "node" (no args) under frontend — not next-server / youtube-svc for p in $(pgrep -x node || true); do cmd=$(tr '\0' ' ' < /proc/$p/cmdline 2>/dev/null) cwd=$(readlink -f /proc/$p/cwd 2>/dev/null || true) echo "node pid=$p cmd=[$cmd] cwd=$cwd" if [ "$cmd" = "node " ] || [ "$cmd" = "node" ]; then kill -9 $p echo killed_orphan_node_$p fi done # leftover next build temp dirs find /opt/douyinWEB/frontend -maxdepth 1 -type d -name '.r*' -mtime -2 2>/dev/null | head -20 rm -rf /opt/douyinWEB/frontend/.r* 2>/dev/null || true systemctl restart douyinweb-web sleep 3 systemctl is-active douyinweb-web echo "=== after ===" uptime ps -eo pid,pcpu,etime,cmd --sort=-pcpu | head -12 curl -sS -m 5 -o /dev/null -w 'health=%{http_code} t=%{time_total}\n' http://127.0.0.1:8014/api/health curl -sS -m 5 -o /dev/null -w 'web=%{http_code}\n' http://127.0.0.1:4015/ echo CLEAN_DONE ''' def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False) _, o, e = c.exec_command( f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@108.174.50.68 " f"bash -s > /tmp/_dy_cpu_clean.out 2>&1 <<'EOF'\n{REMOTE}\nEOF\n" f"echo EXIT:$? >> /tmp/_dy_cpu_clean.out", timeout=1, ) out = "" for i in range(40): time.sleep(2) _, o2, _ = c.exec_command("cat /tmp/_dy_cpu_clean.out 2>/dev/null", timeout=15) out = o2.read().decode("utf-8", "replace") if "CLEAN_DONE" in out or "EXIT:" in out: print(out[-8000:], flush=True) break if i % 5 == 0: print("poll", i, flush=True) # also check dylive for same orphans print("\n===== dylive orphan scan =====", flush=True) _, o, e = c.exec_command( "ps -eo pid,pcpu,etime,cmd --sort=-pcpu | head -12; " "pgrep -ax usr; pgrep -ax node | head -20; uptime", timeout=30, ) print(o.read().decode("utf-8", "replace"), flush=True) c.close() return 0 if __name__ == "__main__": raise SystemExit(main())