Here I needed to kill a c++ process that was leaking memory in order to save the server from meltdown. Very simple while loop that calls the external command “free” and parses it’s output with awk to store a variable. Pretty neat and it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash # Server memory capacity 48GB PKILL=$(which pkill) ECHO=$(which echo) thresHold=47500 BASH=$(which bash) while true do highMem=$(free -m | awk '/^\-/' | awk '{print $3}') $ECHO $highMem if [ $highMem -ge $thresHold ] then $ECHO "We reached the memory limit" $PKILL processName $BASH /opt/code/bin/application_starter.sh > /dev/null 2>&1 else $ECHO "Memory is under control" fi sleep 60 done |
Jason