#!/data/data/com.termux/files/usr/bin/bash
# OpenClaw 守护进程 - 自动重启

while true; do
    # 检查 OpenClaw 是否在运行
    if ! proot-distro login ubuntu -- bash -c 'pgrep -f openclaw' > /dev/null 2>&1; then
        echo "[$(date)] OpenClaw 已停止，正在重启..."
        
        # 清理旧进程
        pkill -9 openclaw 2>/dev/null
        pkill -9 node 2>/dev/null
        
        # 启动 OpenClaw
        proot-distro login ubuntu -- bash -c '
            export NODE_OPTIONS="-r /root/hijack.js"
            cd /root
            nohup openclaw gateway --verbose > /tmp/openclaw.log 2>&1 &
        '
        
        echo "[$(date)] OpenClaw 已重启"
    fi
    
    # 每 30 秒检查一次
    sleep 30
done
