test-repo/scripts/resource-monitor.sh
Matt Bruce 6ddac4a21c Clean up workspace scripts folder
- Removed duplicate monitoring scripts (4 versions consolidated to 1)
- Moved gantt-specific scripts to gantt-board project repo
- Moved create_ios_project.sh into scripts/
- Moved monitor-processes.sh into scripts/ as resource-monitor.sh
- Deleted monitor-restart.sh (duplicate)
- Created README.md documenting what's left and why
2026-02-21 17:21:50 -06:00

81 lines
2.3 KiB
Bash
Executable File

#!/bin/zsh
# Process monitoring script to track what kills Next.js dev servers
# Run this in background to capture system events
LOG_FILE="/Users/mattbruce/.openclaw/workspace/logs/process-monitor.log"
PID_FILE="/tmp/process-monitor.pid"
# Create log directory
mkdir -p "$(dirname $LOG_FILE)"
# Function to log with timestamp
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a $LOG_FILE
}
# Track Node processes
monitor_processes() {
while true; do
# Check if our monitored processes are running
for port in 3000 3003 3005; do
PID=$(lsof -ti:$port 2>/dev/null)
if [ -n "$PID" ]; then
# Get process info
CPU=$(ps -p $PID -o %cpu= 2>/dev/null | tr -d ' ')
MEM=$(ps -p $PID -o %mem= 2>/dev/null | tr -d ' ')
RSS=$(ps -p $PID -o rss= 2>/dev/null | tr -d ' ')
# Log if memory is high (>500MB RSS)
if [ -n "$RSS" ] && [ "$RSS" -gt 512000 ]; then
log "WARNING: Port $port (PID:$PID) using ${RSS}KB RAM (${MEM}% of system)"
fi
# Log if CPU is high (>80%)
if [ -n "$CPU" ] && [ "${CPU%.*}" -gt 80 ]; then
log "WARNING: Port $port (PID:$PID) using ${CPU}% CPU"
fi
fi
done
# Check system memory
FREE_MEM=$(vm_stat | grep "Pages free" | awk '{print $3}' | tr -d '.')
if [ -n "$FREE_MEM" ]; then
FREE_MB=$((FREE_MEM * 4096 / 1024 / 1024))
if [ "$FREE_MB" -lt 500 ]; then
log "WARNING: Low system memory: ${FREE_MB}MB free"
fi
fi
# Check file descriptors
for port in 3000 3003 3005; do
PID=$(lsof -ti:$port 2>/dev/null)
if [ -n "$PID" ]; then
FD_COUNT=$(lsof -p $PID 2>/dev/null | wc -l)
if [ "$FD_COUNT" -gt 900 ]; then
log "WARNING: Port $port (PID:$PID) has $FD_COUNT open file descriptors"
fi
fi
done
sleep 60
done
}
# Check if already running
if [ -f "$PID_FILE" ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
echo "Monitor already running (PID: $(cat $PID_FILE))"
exit 0
fi
# Save PID
echo $$ > $PID_FILE
log "=== Process Monitor Started ==="
log "Monitoring ports: 3000, 3003, 3005"
log "Checking: CPU, Memory, File Descriptors, System Resources"
log "Log file: $LOG_FILE"
# Start monitoring
monitor_processes &