Sunday, May 15, 2011

Automate Swap Memory Actions Bash Script

The Most Important for Our Server?
It is the Memory Monitoring!
If all of the physical memory space used by programs, the Kernel begin to use Swap to allocate more memory.
If all of the swap already in use, the server (or computer) will hang and needs to reboot!!! (You dont want that happens at production servers, dont you?)
Let me share the most important part of my Simple script to prevent high swap memory usage.

Just put it in Crontab every 5 minutes to runs automatically in the background.
Cheers!!!

#Start Script
#!/bin/bash


SERVER=`hostname`


DATE=`date +%Y%m%d_%H%M`


swap=`free -m | awk -F" " '{print $3}' | tail -1`
echo "SWAP Used NOW = $swap";
limit=1000


if [ $swap -gt $limit ];


then


        #This is the Actions if Limit reached
        /etc/init.d/httpd reload;


        echo "$SERVER Swap Memory Warning!";


else


        echo "Swap still safe now.."


fi


#End of Script


Save the script anywhere in the Server, but don't forget to add "chmod +x <your_script>.sh"


If you found this script useful, dont forget to leave some comments :)]

No comments:

Post a Comment