curiousaboutpi
curiousaboutpi

Reputation: 151

Top command: How to stick to one unit (KB/KiB)

I'm using the top command in several distros to feed a Bash script. Currently I'm calling it with top -b -n1.

I'd prefer a unified output in KiB or KB. However, it will display large units in megabytes or gigabytes. Is there an option to avoid these large units?

Please consider the following example:

4911 root      20   0  274m 248m 146m S      0 12.4   0:07.19 example

Edit: To answer 123's question, I transform the columns and send them to a log monitoring appliance. If there's no alternative, I'll convert the units via awk beforehand as per this thread.

Upvotes: 3

Views: 2422

Answers (2)

Armali
Armali

Reputation: 19395

Consider cutting out the middleman top and reading directly from /proc/[1-9]*/statm. All those files consist of one line of numbers, of which the first three correspond with top's VIRT RES SHR, respectively, in units of pages, normally 4096 B, so that by multiplying with 4 you get units of KiB.

Upvotes: 1

user4933750
user4933750

Reputation:

You need a config file. You can create it yourself as $HOME/.toprc or using top interactively. The latter is easy. You just need to press W while top is running in interactive mode.

But first you need to set top interactively to the state you want. To change the memory scale press e until you see what you want. (Then save with W.)

Either way, you need this set in your config: Task_mscale=0 for the lowest scale.

Upvotes: 0

Related Questions