#!/bin/bash SESSIONS_DIR="/root/.openclaw/agents/main/sessions" ALERT_THRESHOLD_MB=8 AUTO_THRESHOLD_MB=5 LOG_DIR="/var/log/openclaw-monitor" ALERT_LOG="${LOG_DIR}/alerts.log" mkdir -p "$LOG_DIR" auto_start_new_session() { local file="$1" size_mb="$2" echo "[AUTO] $(date '+%Y-%m-%d %H:%M:%S') SESSION TOO LARGE: $file (${size_mb}MB > ${AUTO_THRESHOLD_MB}MB) - Auto-starting new session" >> "$ALERT_LOG" local basename=$(basename "$file") cp "$file" "${SESSIONS_DIR}/${basename%.jsonl}_archived_$(date +%Y%m%d_%H%M%S).jsonl" echo "[]" > "$file" NO_PROXY="localhost,127.0.0.1,192.168.0.66" openclaw gateway restart } while true; do for file in "$SESSIONS_DIR"/*.jsonl; do if [ -f "$file" ]; then size_kb=$(stat -c%s "$file" 2>/dev/null || echo 0) size_mb=$(awk "BEGIN {printf \"%.2f\", $size_kb/1024}") if [ "$size_kb" -gt $((ALERT_THRESHOLD_MB * 1024)) ]; then echo "[ALERT] $(date '+%Y-%m-%d %H:%M:%S') SESSION TOO LARGE: $file (${size_mb}MB)" >> "$ALERT_LOG" fi if [ "$size_kb" -gt $((AUTO_THRESHOLD_MB * 1024)) ]; then auto_start_new_session "$file" "$size_mb" fi fi done sleep 300 done