MLUG: [MLUG - DISCUSSION] automatic swap resizing script
[MLUG - DISCUSSION] automatic swap resizing script
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
I know that recently we had a talk about how, and why, Linux didn't do 
this.. but I ran into a situation where I did need it.. so.. here is a 
script that when left running in the background will automaticlly detect 
when you're almost out of memory and will create additional swap space. 
I'm sure it can be improved lots.. this is just a quickie. Create a 
/tmp/esm-stop file to halt the script and remove this emergency swap 
space. Grows your swap in 32MB increments as files in your /tmp 
directory. Should be pretty easy to setup to start with your system and 
cleanup after itself when you shutdown.

#!/bin/bash
# Emergency Swap Monitor
if [[ -z $1 ]]; then
 $0 bg &
 exit
fi
rm /tmp/swap-*
until [[ -e /tmp/esm-stop ]]; do
 SWAP=`top -b -n1 | head -n5 | tail -n1 | tr -s "[:space:]" | cut -d " " 
-f6 | tr "k" " "`
 if [[ $(($SWAP<32768)) == 1 ]]; then
  f=swap-`date "+%s"`
  dd if=/dev/zero of=/tmp/$f bs=1024 count=32768
  mkswap /tmp/$f
  swapon /tmp/$f
 fi
 sleep 15
done
swapoff /tmp/swap*
rm /tmp/swap-*
rm /tmp/esm-stop

--
Michael <EMAIL:PROTECTED>
http://kavlon.org

_______________________________________________
discussion mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/discussion