How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?
Have you tried cat /proc/meminfo
? You can then awk or grep out what you want, MemTotal e.g.
awk '/MemTotal/ {print $2}' /proc/meminfo
or
cat /proc/meminfo | grep MemTotal
If you're interested in the physical RAM, use the command dmidecode
. It gives you a lot more information than just that, but depending on your use case, you might also want to know if the 8G in the system come from 2x4GB sticks or 4x2GB sticks.
cat /proc/meminfo | grep MemTotal
or free gives you the exact amount of RAM your server has. This is not "available memory".
I guess your issue comes up when you have a VM and you would like to calculate the full amount of memory hosted by the hypervisor but you will have to log into the hypervisor in that case.
cat /proc/meminfo | grep MemTotal
is equivalent to
getconf -a | grep PAGES | awk 'BEGIN {total = 1} {if (NR == 1 || NR == 3) total *=$NF} END {print total / 1024" kB"}'