Swap monitoring and auto fix script
So a little while ago I mentioned that you could combine the swap monitoring script and the script that trys to put the swap back into working memory into a script that would just take care of it. I found that my machines were going into swap for trivial reasons, so I just set up a cron to run every 15 minutes that checks for swap, and if it finds any it just throws it back into working memory. Script below:
#!/bin/bash
swap=$(top -bi -n 1 | grep Swap | cut -d ‘,’ -f2 | cut -d “u” -f1 | tr -d ‘ ‘ | tr -d ‘k’)
if [ $swap -ge 0 ] ; then
err=”not enough RAM to write swap back, nothing done”
mem=`free|grep Mem:|awk ‘{print $4}’`
swap=`free|grep Swap:|awk ‘{print $3}’`
test $mem -lt $swap && echo -e $err && exit 1
swapoff -a && swapon -a &&
exit 0
fi