How to choose columns for top command in batch mode?

$ top -bn1 | head

top - 15:16:52 up 11 days,  5:01,  4 users,  load average: 0,00, 0,03, 0,11
Tasks: 411 total,   1 running, 408 sleeping,   0 stopped,   2 zombie
%Cpu(s):  4,5 us,  0,6 sy,  0,0 ni, 94,5 id,  0,4 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem:  16403396 total,  8546712 used,  7856684 free,   711904 buffers
KiB Swap: 16744444 total,        0 used, 16744444 free.  5160648 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   33772   3148   1476 S   0,0  0,0   0:06.04 init
    2 root      20   0       0      0      0 S   0,0  0,0   0:00.03 kthreadd
    3 root      20   0       0      0      0 S   0,0  0,0   0:02.30 ksoftirqd/0

How can I make top return:

  • PID
  • COMMAND
  • %CPU
  • %MEM
  • TIME (not TIME+)

in batch mode?


Solution 1:

Best method I could find:

  • run top in standard mode
  • use F to configure column
  • then use W to save the view as default

This default view will also be used for the batch mode.

Solution 2:

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