#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Fix dual/orphan chrome on physical ops-neko + verify assign trap file.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") FIX = r"""#!/bin/bash set -e echo '=== trap inside neko-3 ===' docker exec douyinweb-neko-3 grep -n on_supervisor_stop /etc/neko/browser-launch.sh | head docker exec douyinweb-neko-3 wc -l /etc/neko/browser-launch.sh wc -l /opt/douyinWEB/backend/neko/browser-launch.sh fix_one() { local N="$1" echo "===== $N =====" docker exec "$N" supervisorctl stop google-chrome || true sleep 1 docker exec -i "$N" python3 - <<'PY' import os, signal, time n=0 for _ in range(3): for pid in list(os.listdir("/proc")): if not pid.isdigit(): continue try: raw=open(f"/proc/{pid}/cmdline","rb").read() except: continue if not raw: continue if b"google-chrome" in raw or b"/opt/google/chrome" in raw: try: os.kill(int(pid), signal.SIGKILL); n+=1 except Exception: pass time.sleep(0.4) print("killed", n) PY docker exec "$N" bash -lc 'rm -f /home/neko/.config/google-chrome/SingletonCookie /home/neko/.config/google-chrome/SingletonLock /home/neko/.config/google-chrome/SingletonSocket /home/neko/.config/google-chrome/SingletonStartupLock /home/neko/.config/google-chrome/lockfile /home/neko/.config/google-chrome/DevToolsActivePort 2>/dev/null; true' docker exec "$N" supervisorctl start google-chrome } for N in douyinweb-ops-neko-1 douyinweb-ops-neko-2; do docker inspect "$N" >/dev/null 2>&1 && fix_one "$N" done # bounce assign chrome so browser-launch process reloads trap from bind-mount for i in 1 2 3 4 5 6 7 8; do N=douyinweb-neko-$i docker inspect "$N" >/dev/null 2>&1 || continue fix_one "$N" done echo '===== wait =====' sleep 22 for N in douyinweb-ops-neko-1 douyinweb-ops-neko-2 \ douyinweb-neko-1 douyinweb-neko-2 douyinweb-neko-3 douyinweb-neko-4 \ douyinweb-neko-5 douyinweb-neko-6 douyinweb-neko-7 douyinweb-neko-8; do docker inspect "$N" >/dev/null 2>&1 || continue out=$(docker exec -i "$N" python3 - <<'PY' import os, json, urllib.request mains=0; load=False; sw=-1; trap=False try: trap=b"on_supervisor_stop" in open("/etc/neko/browser-launch.sh","rb").read() except Exception as e: trap=str(e) for pid in os.listdir("/proc"): if not pid.isdigit(): continue try: raw=open(f"/proc/{pid}/cmdline","rb").read() except: continue if not raw: continue if b"google-chrome" in raw and b"--type=" not in raw and b"crashpad" not in raw and b"browser-launch" not in raw: mains += 1 if b"load-extension" in raw and b"dhdgff" in raw: load=True try: tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=3).read()) sw=sum(1 for t in tabs if t.get("type")=="service_worker" and "dhdgff" in (t.get("url") or "")) except Exception: sw=-1 print(f"mains={mains} load_ext={load} tm_sw={sw} trap={trap}") PY ) echo "$N $out" done """ def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( "107.173.30.245", 4014, "eric", "4669805", timeout=45, allow_agent=False, look_for_keys=False, banner_timeout=120, ) sftp = c.open_sftp() with sftp.file("/tmp/_fix_ops_and_bounce_assign.sh", "w") as f: f.write(FIX) sftp.close() _i, o, e = c.exec_command( "echo '4669805' | sudo -S -p '' bash /tmp/_fix_ops_and_bounce_assign.sh", timeout=420, ) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() if __name__ == "__main__": main()