//View Tip #892
Similar Tips
» Remove every file but one
» Calendar with today colored [handle single digit dates]
» Kill matching processes
» Uppercase to lowercase with tr
» Splitting arguments with read

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Want to check the amount of used, free and total memory and swap from the command line? This script displays memory and swap information. Fully posix compliant and should work with all 2.[2-6].* kernels .

#fetch and process memory information
[ -f /proc/meminfo ] && {
Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
}
MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))"
MEMTOTAL="$(( $MemTotal / 1024))"
MEMFREE="$(( $MEMTOTAL - $MEMUSED ))"
MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))"
[ "$SwapTotal" -gt "1" ] && {
  SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))"
  SWAPTOTAL="$(( $SwapTotal / 1024))"
  SWAPFREE="$(( $SWAPTOTAL - $SWAPUSED ))"
  SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))" 
} || {
  SWAPUSED="0"
  SWAPTOTAL="0"
  SWAPPER="0" 
}

# display the information
/bin/echo
/bin/echo "Memory"
/bin/echo "Used: $MEMUSED"
/bin/echo "Free: $MEMFREE"
/bin/echo "Total: $MEMTOTAL"
/bin/echo
/bin/echo "Swap"
/bin/echo "Used: $SWAPUSED"
/bin/echo "Free: $SWAPFREE"
/bin/echo "Total: $SWAPTOTAL"
/bin/echo


View Comments »




Comments 

Add your comment

Comments are currently disabled
Why is this better than `free -mt`?
Posted 2009-10-26 03:38:52
Herman Merman
Or, er, cat /proc/meminfo
Posted 2009-10-26 04:07:09
Michael
Why is is better? Maybe it isn't "better" in an efficiency sense, but if you're new to shell scripting, these exercises demonstrate a way to access system information the script writer might find useful. Besides, why do so many of us find it necessary to eat our own instead of being thankful?

Thanks shell-fu for taking the time.
Posted 2009-10-26 06:15:02
Yohann
I found this script on the internet, so I cannot in any way claim myself as its creator. It clears memory up and moves any SWAP to the RAM, while displaying memory usage before, switching, and then after. It has to be run with sudo or as root. Helps me out a lot.

-----------------------------
#!/bin/sh
echo -e "BEFORE EMPTY CACHES"
free
echo 3 > /proc/sys/vm/drop_caches
echo 0 > /proc/sys/vm/drop_caches
msg="Cannot write swap back to RAM...\nNot enough memory - bye..."
mem=`free|grep Mem:|awk '{print $4}'`
swap=`free|grep Swap:|awk '{print $3}'`
test $mem -lt $swap && echo -e $msg && exit 1
echo -e "\nOutput of free before moving swap:" &&
free &&
swapoff -a && swapon -a &&
echo -e "\nOutput of free after:" &&
free &&
exit 0
-----------------
Posted 2009-10-27 23:04:18

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit