15

I've seen a few posts wanting the same regarding usage, however the answers ALWAYS refer to installing some third party tool.

I do not want to install anything.

Any one liners? The following would be a nice output:

CPU 11% RAM 20% HDD 85%
4
  • Is HDD usage the percentage how full it is or how busy it is? – Byte Commander Aug 1 '17 at 15:06
  • @ByteCommander Both? :) On a serious note, the capacity though. If both that would be nice. – Karl Morrison Aug 1 '17 at 15:08
  • Sigh....... :-/ – Byte Commander Aug 1 '17 at 15:09
  • @ByteCommander By capacity I mean "how full it is" if I wasn't clear :) – Karl Morrison Aug 1 '17 at 15:21
31

You can use this

echo "CPU `LC_ALL=C top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'`% RAM `free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }'` HDD `df -h / | awk '/\// {print $(NF-1)}'`"

The output is

CPU 7.4% RAM 33.9% HDD 94%
11
  • 2
    the HD part is going to suck if you have many partitions. – coteyr Aug 1 '17 at 20:50
  • 1
    df -h / part of command line shows the statistics about / partition. You can repeat df -h command and substitute / with the mount point you need to monitor. – MKay Aug 2 '17 at 6:56
  • 1
    Doesn't work for me. CPU is always 91%, I have no idea where it gets that from. // And RAM includes buffers and cache making it look like it's exhausted. I do use 97.8% RAM, but there's still 67.5% free. – Oskar Skog Aug 2 '17 at 9:31
  • 2
    @OskarSkog Your top is alright. CPU usage can not be measured at the current point in time, it must be measured over a short timespan. When top runs the first iteration, it shows the average usage since the last reboot. Starting with the second iteration it shows the actual current usage since the previous iteration. And if you have problems with localization, you can put LC_ALL=C in front of the command to use default English localization (number formats etc, not language). – Byte Commander Aug 2 '17 at 10:59
  • 2
    @MKay: You may want to replace top -bn1 | grep "Cpu(s)" with top -bn2 | grep "Cpu(s)" | tail -n1. The former will display a long term average as pointed out by Byte Commander. – Oskar Skog Aug 2 '17 at 11:31
8

For CPU usage (average of (user+system)/(user+system+idle) times over 0.1 seconds):

(grep 'cpu ' /proc/stat;sleep 0.1;grep 'cpu ' /proc/stat)|awk -v RS="" '{print "CPU "($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)"%"}'

For RAM usage ((total-available)/total):

awk '/MemTotal/{t=$2}/MemAvailable/{a=$2}END{print 100-100*a/t"%"}' /proc/meminfo

For HDD usage (only of the volume mounted as /):

df | awk '/ \/$/{print "HDD "$5}'
4
  • Interesting, there's less memory available than what `free -m | head -n3 | tail -n1' reports as free memory. – Oskar Skog Aug 2 '17 at 9:39
  • @OskarSkog Can you share your output of that command? For me it simply prints the Swap total/used/free line, which is not relevant here. Maybe you meant something else, or your free command still uses the old output format with separate -/+ buffers/cache line... – Byte Commander Aug 2 '17 at 10:55
  • My free uses the old format: oskog97.com/sshin/free-output.png What's changed? – Oskar Skog Aug 2 '17 at 11:26
  • 3
    @OskarSkog The "-/+ buffers/cache" line got removed and now there is an additional column "available" in the "Mem" row instead. From man free: "Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)" – Byte Commander Aug 2 '17 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.