#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") SCRIPT = r"""#!/bin/bash set +e python3 <<'PY' import sqlite3, time, signal, os, subprocess con=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db') con.row_factory=sqlite3.Row now=time.time() rows=con.execute("select group_id,status,ffmpeg_pid,push_seconds,error from yt_relay_states where status in ('pushing','starting','streaming')").fetchall() print('pushing_rows', len(rows)) for r in rows: d=dict(r) print(d) pid=int(d.get('ffmpeg_pid') or 0) if pid>0: try: os.kill(pid, signal.SIGTERM) print('SIGTERM', pid) except Exception as e: print('kill_err', e) con.execute( "update yt_relay_states set status=?, error=?, ffmpeg_pid=0, speed=0, frame=0, updated_at=? where group_id=?", ('waiting', '已停止:Studio NODATA/待开播(未 on_air)无效推流', now, d['group_id']), ) con.commit() # leftover ffmpeg youtube out=subprocess.check_output("ps -ef | grep -E '[f]fmpeg' | grep -iE 'rtmp|youtube' || true", shell=True, text=True) print('ffmpeg_left', out[:800] or 'none') for line in out.splitlines(): parts=line.split() if len(parts)>1 and parts[1].isdigit(): try: os.kill(int(parts[1]), signal.SIGKILL) print('SIGKILL ffmpeg', parts[1]) except Exception as e: print(e) PY curl -sS -m 5 http://127.0.0.1:8014/api/health | head -c 160; echo python3 <<'PY' import sqlite3 con=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db') for r in con.execute("select group_id,status,ffmpeg_pid,substr(error,1,80) from yt_relay_states where group_id like '3fb8%' or status!='paused_exclusive' order by updated_at desc limit 6"): print(r) PY """ def main(): 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) sftp = c.open_sftp() with sftp.file("/tmp/_stop_invalid2.sh", "w") as f: f.write(SCRIPT) sftp.close() _i, o, e = c.exec_command("bash /tmp/_stop_invalid2.sh", timeout=45) o.channel.settimeout(40) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() if __name__ == "__main__": main()