#!/bin/bash # Kill the process tree using the most CPU (excluding critical ones) # Find the top CPU consumer (excluding protected and transient processes) TOP_LINE=$(ps -eo pid,comm,%cpu --sort=-%cpu | grep -v -E '(PID|systemd|sshd|monit|earlyoom|bash|ps|awk|grep|head)' | head -1) TARGET_PID=$(echo "$TOP_LINE" | awk '{print $1}') COMM=$(echo "$TOP_LINE" | awk '{print $2}') if [ -n "$TARGET_PID" ] && [ -n "$COMM" ]; then logger "monit cpu-killer: Killing all '$COMM' processes (detected high CPU on PID $TARGET_PID)" # Kill all processes with this command name pkill -9 -x "$COMM" fi