0

How can I find determine real-time system performance parameters such as memory usage, CPU usage, disk usage from the terminal in Linux. I have asked Google but have not found a succinct suggestion. I am trying to divorce myself from the distro bundled utilities and do this from terminal

sourcejedi
  • 50,249

3 Answers3

1

Many of the commands you want "do one thing, and do it well". On the one hand, this means they all look a bit different. On the bright side, this can make it more manageable - to learn one thing at a time :-). I will try to answer directly and succinctly. You asked for the top three:

  1. df -h - filesystem usage levels. This includes in-memory filesystems (tmpfs).
  2. free -h - for remaining "available" memory, and how much swap is used. The other fields might not mean what you think, sorry! See below.
  3. top - show processes sorted by cpu usage.

df and free just run once, and print the current results. If you want to see a "live" view of the current results in a terminal, put watch in front of the command. To stop watch, press Ctrl+c.

4. You didn't mention disk activity, i.e. reads/writes. But this is also shown, per-process, in recent versions of gnome-system-monitor. Heavy disk activity can sometimes make the system feel very slow. To see this information on the command-line, use sudo iotop.

iotop is usually not installed by default. If a program is not installed, Linux Mint (and other distributions) will offer to install it, when you try to run it!

In theory, you might want to see both current values, and previous history. LIke the graphs in gnome-system-monitor. There is a slightly less common command for this. Use vmstat 5 to see, over 5 second intervals: usage of swap and memory, swap activity, disk activity (io), and cpu activity. You will probably need to resize the terminal window, to make it a little wider :-).

Understanding Linux memory usage

This can get a bit tricky. I am stealing the quote block below from linuxatemyram.com.

(I think this is a good example where using the common, single-purpose tool can help focus your learning. And when you have a specific question, it might already be documented. E.g. see the manual page man free. Or the support communities have likely already answered it somewhere :-).

A healthy Linux system with more than enough memory will, after running for a while, show the following expected and harmless behavior:

  • free memory is close to 0
  • used memory is close to total

    EDIT: this part is outdated :-(. used no longer includes buff/cache. You can just ignore used, because either way it is redundant with the other fields!

  • available memory (or "free + buffers/cache") has enough room (let's say, 20%+ of total)
  • swap used does not change

Warning signs of a genuine low memory situation that you may want to look into:

  • available memory (or "free + buffers/cache") is close to zero
  • swap used increases or fluctuates
  • dmesg | grep oom-killer shows the OutOfMemory-killer at work

Per-process memory usage

Per-process memory usage can also be tricky! top shows per-process memory usage. But top is misleading for some types of program, including firefox and google-chrome. This is because top counts shared memory multiple times, in every process that shares that memory :-).

sudo smem -t shows better memory usage numbers, in the PSS column.

sudo smem -t -P firefox is a pretty neat command. It adds up how much memory my favourite browser is eating.

Using sudo here allows smem to work on running processes that are not owned by your login user. If you don't need that, you can run it without sudo.

Propaganda for atop

atop is like a swiss army chainsaw multi-tool. It can show most of the above information, apart from disk space usage.

For example: sudo atop -R enables the PSIZE column, which is equivalent to the smem column PSS. Press m to show this memory information. Press Shift+m to sort by memory usage.

In general, the top half of atop tries to show the most important information and highlight potential concerns. It leaves out the values which are currently less significant, to make the best use of the screen space.

sourcejedi
  • 50,249
  • I already use ALL of the commands you mention. Apparently you missed the entire point if my question. Succinct is the key. I don't have time to go through all the top or htop data. Same for df, du. I have 3 monitors and 8 workspaces so screen real estate isn't a problem. Winnowing this values down to the most germane values us. What YOU think may be immaterial to me and likely is. – Buzzsaw48 Jul 02 '19 at 04:23
  • @Buzzsaw48 I see. It is not clear from the original written question. This site is a bit weird. The site design discourages extended discussion, and encourages to-the-point answers which could be useful to many. A common question comment is "what have you tried?", aka Tell us what you found and why it didn’t meet your needs. It doesn't look like anyone particularly liked the existing answers, but there's enough of them that it might be clearer to ask again, starting from scratch. – sourcejedi Jul 02 '19 at 08:00
  • @Buzzsaw48 Since atop feels to me like a good potential answer, you might focus on explaining what you want, that the atop command you already use does not have or does not show in the desired way... – sourcejedi Jul 02 '19 at 08:01
1

Use the command top to display real-time information about active processes. By default top shows units in kilobytes but you can change this by using ShiftE to change the units of the summary information above the list of current processes and e to change the units of the list of currently running processes.

Pressing Shiftm sorts the list by memory or you can press Shiftf to open a field management window where you can select which column header to use to sort (s)the list in descending order

As for disk usage you can use the commands df and du:

  • df shows the available space on the file system (e.g. ntfs, ext4, fat32) that contains each file name argument. For example, if you want to see the free space on an external hard drive use the command df -h /Path/to/external_harddrive which will output the available space on the file system of the external drive mounted on the directory /Path/to/external_harddrive. If no file is specified then df will show the available space on all mounted file systems
  • du will show the disk usage of the specified file. When a directory is specified as an argument to du, du will show the disk usage of the directory recursively which means du will show the disk usage in a depth-first traversal (process each directory's content before the directory itself) again and again until it has shown the disk usage of all files or directories in the specified directory. Use the option --max-depth=LEVEL to make du only show the disk usage of all files or directories the specified level or below the directory given to du as an argument. For example du -h --max-depth=1 ~/Documents will printout the size of all files and directories 1 level or less below the directory Documents (level 1 is the lowest level below a directory tree). If you don't want the size of all files and directories to be shown use the -s(summary) option to only show the total size of the Documents directory without the individual sizes of the Document directory's contents

Note: the short option -h (--human-readable in long form) means human readable which makes the output of du or df easier for the human brain to process into something meaningful

bit
  • 1,106
0

Kernel writes the memory/cpu/etc usage to files under /proc directory. For example, you can get the realtime memory usage in the file /proc/meminfo.

[root@localhost ~]# cat /proc/meminfo MemTotal: 131832516 kB MemFree: 79406996 kB MemAvailable: 121444792 kB Buffers: 2112 kB Cached: 41602056 kB SwapCached: 0 kB Active: 30971812 kB Inactive: 18976428 kB Active(anon): 8353396 kB Inactive(anon): 8332 kB Active(file): 22618416 kB Inactive(file): 18968096 kB

Like this, proc/stat file will give you a pathetic amount of details about your CPU. Explore the /proc directory, you will find more.