xiaowei-system/scripts/bge_mem_check.sh

25 lines
856 B
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# bge_embed_server 内存泄漏监控脚本watchdog pattern
# 内存超过 2GB 时通过 systemd 重启,正常时静默(但 cron 需要非空 stdout
set -o pipefail
PID=$(pgrep -f "bge_embed_server.py" 2>/dev/null | grep -v "$$" | head -1)
if [ -z "$PID" ]; then
systemctl --user start bge-embed.service 2>/dev/null
echo "bge: not running, started via systemd"
exit 0
fi
RSS=$(ps -o rss= -p "$PID" 2>/dev/null | tail -1 | tr -d ' ')
MB=${RSS:-0}
MB=$((MB / 1024))
if [ "$MB" -gt 2048 ]; then
LOG=~/.hermes/daemon/bge-restart.log
echo "[$(date '+%Y-%m-%d %H:%M:%S')] bge_embed 内存 ${MB}MB > 2GBsystemd 重启" >> "$LOG"
systemctl --user restart bge-embed.service
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 重启完成" >> "$LOG"
echo "bge: restarted (${MB}MB > 2048MB)"
fi
echo "bge: ${MB}MB OK"