Files
runaway-process-killer/scripts/test.sh
2026-01-22 18:46:43 -05:00

106 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Test script for runaway process killer
# WARNING: This will stress your system!
set -e
echo "============================================"
echo "Runaway Process Killer - Test Suite"
echo "============================================"
echo ""
echo "WARNING: This will temporarily stress your CPU and RAM!"
echo ""
if [ "$EUID" -ne 0 ]; then
echo "Error: Please run as root (sudo ./test.sh)"
exit 1
fi
# Check stress is installed
if ! command -v stress &> /dev/null; then
echo "Installing stress tool..."
apt-get install -y -qq stress
fi
# Backup current config
ORIG_CYCLES=$(grep "for.*cycles" /etc/monit/conf.d/cpu-killer | grep -oP '\d+(?= cycles)')
echo "Current CPU threshold: ${ORIG_CYCLES} cycles"
echo "Temporarily setting to 2 cycles for testing..."
# Set to 2 cycles for testing
sed -i "s/for ${ORIG_CYCLES} cycles/for 2 cycles/" /etc/monit/conf.d/cpu-killer
monit reload
sleep 2
echo ""
echo "=== TEST 1: CPU Protection ==="
echo "Starting CPU stress (expect kill in ~2 minutes)..."
echo ""
stress --cpu 4 --timeout 300 &
STRESS_PID=$!
sleep 2
START=$(date +%s)
while kill -0 $STRESS_PID 2>/dev/null; do
sleep 5
NOW=$(date +%s)
ELAPSED=$((NOW - START))
if [ $ELAPSED -ge 180 ]; then
echo "FAILED: CPU stress not killed after 3 minutes"
kill $STRESS_PID 2>/dev/null || true
break
fi
if ! pgrep -x stress > /dev/null 2>&1; then
echo "PASSED: CPU stress killed after ${ELAPSED}s"
break
fi
[ $((ELAPSED % 30)) -eq 0 ] && echo "[${ELAPSED}s] Stress still running..."
done
sleep 2
echo ""
echo "=== TEST 2: RAM Protection ==="
echo "Starting RAM stress (expect quick kill)..."
echo ""
stress --vm 4 --vm-bytes 4G --vm-keep --timeout 300 &
STRESS_PID=$!
sleep 2
START=$(date +%s)
while pgrep -x stress > /dev/null 2>&1; do
sleep 5
NOW=$(date +%s)
ELAPSED=$((NOW - START))
if [ $ELAPSED -ge 120 ]; then
echo "FAILED: RAM stress not killed after 2 minutes"
pkill stress 2>/dev/null || true
break
fi
if ! pgrep -x stress > /dev/null 2>&1; then
echo "PASSED: RAM stress killed after ${ELAPSED}s"
break
fi
MEM_FREE=$(free -m | awk '/Mem:/ {print $7}')
echo "[${ELAPSED}s] Stress running, ${MEM_FREE}MB free"
done
# Restore original config
echo ""
echo "Restoring original CPU threshold (${ORIG_CYCLES} cycles)..."
sed -i "s/for 2 cycles/for ${ORIG_CYCLES} cycles/" /etc/monit/conf.d/cpu-killer
monit reload
echo ""
echo "============================================"
echo "Tests Complete"
echo "============================================"