Reputation: 779
Currently creating a script that will record CPU and memory currently being used every second until you hit a key, which will then graph the results via gnuplot. I cannot seem to figure out how to pull the correct percentage of memory being used. The command I currently have down is...
echo "(free | grep Mem | awk '${print $3/$2 * 100"%"}')"
This displays the current amount of memory that is free and gives me the following results... ex.
97.0305%
98.06%
94.07%
87.950%
I do not need to have the amount that is free being displayed, but the current amount being used. It should be pretty simple to take that number and subtract it from 100 (setting it as a variable or something), but what I would like would be to keep it in a one line echo command like I have above. Any suggestions? Thank you
Upvotes: 0
Views: 97
Reputation: 2258
$ free
total used free shared buffers cached
Mem: 15360000 5837240 9522760 0 1202040 2490640
-/+ buffers/cache: 2144560 13215440
Swap: 1048568 0 1048568
Why not use the 4th column (free) instead? Also, you don't need that grep. Example:
$ while true; do awk '/Mem/{printf "\r"$4/$2*100"%"}' <(free); done
62.0591%
** note use the \r only if you want to overwrite the previous output and don't use "while true" - it's just an example.
Happy hacking!
EDIT: I completely misread your question, disregard me :(
This will provide you with the current actual usage, according to free, with the buffers/cache removed:
awk '/Mem/{t=$2;getline;print $3/t*100"%"}' <(free)
13.9609%
Upvotes: 2
Reputation: 37318
Sure, it's easy
echo "(free | grep Mem | awk '${print (100 - ( ($3/$2) * 100) ) "%"}')"
and I don't think you need the echo "..."
wrapper, try
free | grep Mem | awk '{print (100 - ( ($3/$2) * 100) ) "%"}'
and you don't need the grep ;-) , try
free | awk '/Mem/{print (100 - ( ($3/$2) * 100) ) "%"}'
and to have the absolute minimum of code, as Karoly Horvath reminds us, (in this case) "All the parentheses are unnecessary", you can use
free | awk '/Mem/{print 100 - $3/$2 * 100 "%"}'
Sorry, but I don't have a system I can test this on right now.
IHTH
Upvotes: 2