Process monitor.sh
Od HLDS.pl
#!/bin/bash # This script assumes your top configuration is default. # Who to e-mail when a process is killed. contact="escapedturkey@escapedturkey.com" # Define the top command based load threshold percentage. load="25" # Define location of appending log file. archive="/var/log/killed.log" # Cycle 1 -- Checking to see if a process is using too much CPU. cycle1=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v bin | grep -v daemon | grep -v adm | grep -v lp | grep -v sync | grep -v shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp | grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v rpc | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v smmsp | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: | grep -v Cpu7 | grep -v Cpu6 | grep -v Cpu5 | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 | grep -v Cpu1 | grep -v Cpu0 | grep -v Tasks | grep -v top | awk '$9 >= '$load' { print $1 }' | tail -n 1`) # If nothing is found, exit. if [ -z "$cycle1" ]; then # Clean-up files upon exit. exit 0 fi # Pausing for a bit. The process may be just having a temporary spike due to a restart or map change. sleep 60 # Cycle 2 -- Checking again to see if the process is still using too much CPU. cycle2=(`top -c -b -n 1 | grep -v grep | grep -v root | grep -v bin | grep -v daemon | grep -v adm | grep -v lp | grep -v sync | grep -v shutdown | grep -v halt | grep -v mail | grep -v news | grep -v uucp | grep -v operator | grep -v games | grep -v gopher | grep -v ftp | grep -v nobody | grep -v dbus | grep -v vcsa | grep -v rpm | grep -v haldaemon | grep -v netdump | grep -v nscd | grep -v sshd | grep -v rpc | grep -v rpcuser | grep -v nfsnobody | grep -v mailnull | grep -v smmsp | grep -v pcap | grep -v xfs | grep -v Mem: | grep -v Swap: | grep -v Cpu7 | grep -v Cpu6 | grep -v Cpu5 | grep -v Cpu4 | grep -v Cpu3 | grep -v Cpu2 | grep -v Cpu1 | grep -v Cpu0 | grep -v Tasks | grep -v top | awk '$9 >= '$load' { print $1 }' | tail -n 1`) # Comparing to see if the same process is using too much CPU, if so kill the process, if not, exit the script. if [ "$cycle1" = "$cycle2" ]; then # Logs killed process top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 >> $archive # E-mail killed process top -c -b -n 1 | grep "$cycle2" | grep -v grep | head -n 1 | mail -s "Excessive CPU Process killed at $HOSTNAME" $contact # Kill the process kill -9 "$cycle2" fi exit 0 #EOF