Can we get specific column in a top command, for eg I'm interested in only the memory utilization and CPU usage column.
How do I reduce the displayed columns of the top command to only the above two columns?
Can we get specific column in a top command, for eg I'm interested in only the memory utilization and CPU usage column.
How do I reduce the displayed columns of the top command to only the above two columns?
NOTE: Assuming you have this version of top
(procps). You can check with this command:
$ top --version
top: procps version 3.2.8
usage: top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
procps is often the version of top
included with Fedora/CentOS/RHEL and other variants.
If you look in the man page for top
you'll see a section titled: "2b. SELECTING and ORDERING Columns". There are keyboard shortcuts for toggling visibility for the different fields/columns.
For example:
f,o . Fields/Columns: 'f' add or remove; 'o' change display order
F or O . Select sort field
<,> . Move sort field: '<' next col left; '>' next col right
You can use the key f while in top
to get to a secondary screen where you can specify which columns should be toggled visible or not:
For example:
Current Fields: ANOPQRSTUVbcdefgjlmyzWHIKX for window 3:Mem
Toggle fields via field letter, type any other key to return
* A: PID = Process Id * W: S = Process Status
* N: %MEM = Memory usage (RES) * H: PR = Priority
* O: VIRT = Virtual Image (kb) * I: NI = Nice value
* P: SWAP = Swapped size (kb) * K: %CPU = CPU usage
* Q: RES = Resident size (kb) * X: COMMAND = Command name/line
* R: CODE = Code size (kb)
...
There are more, these are just a sample. When you're done toggling the columns the way you want, use the Esc to get out of the selection screen.
You can use the Shift+W to save your changes so they're the defaults:
W Write configuration file
The file is stored here, $HOME/.toprc
, and looks like this:
$ more .toprc
RCfile for "top with windows" # shameless braggin'
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=1.000, Curwin=2
Def fieldscur=AEHIoqTWKNMBcdfgjpLrsuvyzX
winflags=129016, sortindx=19, maxtasks=0
summclr=2, msgsclr=5, headclr=7, taskclr=7
Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
winflags=63416, sortindx=13, maxtasks=0
summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
winflags=65464, sortindx=13, maxtasks=0
summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX
winflags=65464, sortindx=12, maxtasks=0
summclr=3, msgsclr=3, headclr=2, taskclr=7
See section 5 of the man page for more details, "5. FILES".
[Centos-6 | Ubuntu 12.10] This cmd print 4 top processes sort by CPU USAGE
top -bn 1 | grep "^ " | awk '{ printf("%-8s %-8s %-8s\n", $9, $10, $12); }' | head -n 5
Output
%CPU %MEM COMMAND
7.7 0.2 top
0.0 0.3 init
0.0 0.0 kthreadd
0.0 0.0 migration/0
Note: head -n 5 instead of 4 because we also have column name
columns $9, $10, $12 means CPU, MEM, COMMAND. Use 'top' command to get column numbers
Sort by MEMORY USAGE (your 'top' must support -m to run this)
# this work on my centos-6 machine, NOT work on my Ubuntu 12.10
top -m -bn 1 | grep "^ " | awk '{ printf("%-8s %-8s %-8s\n", $9, $10, $12); }' | head -n 5
to view in json format and removing header as well,
top -bn 1 | grep "^ " | awk '{ printf("%s%s%s\n","{CPU:"$9",","MEM:"$10",","CMD:"$12"}"); }' | head -n 6 | tail -n +2
output like below,
{CPU:6.4,MEM:0.3,CMD:gnome-terminal}
{CPU:6.4,MEM:1.9,CMD:chrome}
{CPU:0.0,MEM:0.0,CMD:init}
{CPU:0.0,MEM:0.0,CMD:kthreadd}
{CPU:0.0,MEM:0.0,CMD:ksoftirqd/0}
grep | awk | head | tail
can be coalesced into a single awk
.
– Chris Davies
Aug 16 '18 at 20:19
top -stats pid,cpu,threads,mem,purg,vsize,time,state
man top
and look for -o
for the list of supported keys (=columns).
Command line configuration
I did not want to affect my whole user by overwriting the user .toprc
, so I came up with an solution to have a separate configuration per use-case.
You can make top
use a custom configuration file instead of the one in your users home directory.
Just change the HOME
variable to a custom directory containing the .toprc
file for your needs.
# TOPRC_PROFILE_DIRECTORY contains .toprc (or can be created by saving using shift+w after configuring using keybindings)
TOPRC_PROFILE_DIRECTORY="<your-directory>"
HOME="$TOPRC_PROFILE_DIRECTORY" top
top
variant? There are dozens of top implementations. – Stéphane Chazelas Aug 13 '13 at 16:04top -h
). – slm Aug 13 '13 at 17:09