Swap monitoring and auto fix script

Posted by on November 15, 2011 in Bash, Operating Systems, Programming, Ubuntu | 0 comments

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:


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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment

Your email address will not be published. Required fields are marked *