On Linux(also true to UNIX), top command is one of the most frequently used commands in our daily system administrative jobs. top command displays processor activity of your Linux box and also displays tasks managed by kernel in real-time. It’ll show processor and memory are being used and other information like running processes. This may help you to take correct action. 

Here are some of Top Command Examples:

Display of Top Command

# top
top - 11:39:25 up 26 days, 21:24,  3 users,  load average: 8.49, 8.54, 7.98
Tasks: 304 total,   6 running, 298 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.1 us, 28.9 sy,  0.0 ni, 23.6 id, 32.6 wa,  0.0 hi,  2.8 si,  0.0 st
KiB Mem : 16268500 total,   170412 free,  4758548 used, 11339540 buff/cache
KiB Swap:  2097148 total,  2022576 free,    74572 used. 10506132 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 4584 root      20   0 4633036 497584   1432 S  18.5  3.1 167:25.53 java
24261 root      20   0   21956   1140    928 R  16.6  0.0   0:00.66 tar
 4528 root      20   0 4630856 337884   3320 S  15.9  2.1 140:28.81 java
 2101 vtl       20   0   19396   2736    480 R  15.2  0.0 138:10.73 vtltape
...

In the example above, it shows information like tasks, memory, cpu and swap. Press ‘q‘ to quit window.

Display All CPUs / Cores in the Top Output

Top output by default shows CPU line for all the CPUs combined together, press '1'(one), all CPUs will shown below.

top - 13:25:29 up 26 days, 23:10,  3 users,  load average: 8.53, 8.65, 8.70
Tasks: 312 total,   4 running, 308 sleeping,   0 stopped,   0 zombie
%Cpu0  : 37.3 us, 10.0 sy,  0.0 ni, 23.7 id, 28.0 wa,  0.0 hi,  1.0 si,  0.0 st
%Cpu1  : 39.2 us,  9.1 sy,  0.0 ni, 21.6 id, 29.4 wa,  0.0 hi,  0.7 si,  0.0 st
%Cpu2  : 15.4 us, 16.1 sy,  0.0 ni,  4.4 id, 62.4 wa,  0.0 hi,  1.7 si,  0.0 st
%Cpu3  : 13.0 us, 10.0 sy,  0.0 ni, 11.4 id, 64.2 wa,  0.0 hi,  1.3 si,  0.0 st
KiB Mem : 16268500 total,   161376 free,  4779628 used, 11327496 buff/cache
KiB Swap:  2097148 total,  2016148 free,    81000 used. 10487572 avail Mem
...

Show Processes Sorted by memory usage, -M

By default top command displays the processes in the order of CPU usage.  When the top command is running, press M (upper-case) to display processes sorted by memory usage as shown below.

top - 13:23:28 up 26 days, 23:08,  3 users,  load average: 8.82, 8.75, 8.74
Tasks: 323 total,   1 running, 322 sleeping,   0 stopped,   0 zombie
%Cpu(s): 10.1 us, 33.1 sy,  0.0 ni, 24.6 id, 29.3 wa,  0.0 hi,  3.0 si,  0.0 st
KiB Mem : 16268500 total,   167908 free,  4807552 used, 11293040 buff/cache
KiB Swap:  2097148 total,  2016148 free,    81000 used. 10459168 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 4410 root      20   0 4661988 1.045g   1896 S   1.0  6.7  40:20.34 java
 4130 root      20   0 4718704 855656   1320 S   6.0  5.3 187:17.40 java
 4584 root      20   0 4633036 491988    748 S  10.3  3.0 183:07.09 java
 1887 postgres  20   0 1337752 378940 377560 S   0.0  2.3   1:26.37 postgres 

Display Specific User Process

Use top command with ‘u‘ option will display specific User process details.

run top, then type 'u', you will see a prompt ask you to enter user name:

Which user (blank for all) 

Or, run

# top -u vtl          #vtl is  a user name

Highlight Running Process

Press ‘z‘ option in running top command will display running process in color which may help you to identified running process easily.

Press 'Z'(uppercase), you will get a prompt window let you chose which color

1) Select a target as an upper case letter, current target is  T :
   S = Summary Data,  M = Messages/Prompts,
   H = Column Heads,  T = Task Information
2) Select a color as a number, current color is  1 :
   0 = black,  1 = red,      2 = green,  3 = yellow,
   4 = blue,   5 = magenta,  6 = cyan,   7 = white

3) Then use these keys when finished:
   'q' to abort changes to window '1:Def'
   'a' or 'w' to commit & change another, <Enter> to commit and end

Change Delay or Set ‘Screen Refresh Interval’ in Top

By default screen refresh interval is 3.0 seconds, same can be change pressing ‘d‘ option in running top command and change it as desired as shown below.

Change delay from 3.0 to 

Shows Absolute Path of Processes

Press ‘c‘ option in running top command, it will display absolute path of running process.

Kill running process with argument ‘k’

You can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window as shown below.

PID to signal/kill [default pid = 4584] 

Sort by CPU Utilisation

Press (Shift+P) to sort processes as per CPU utilization.

Renice a Process

You can use ‘r‘ option to change the priority of the process also called Renice.

PID to renice [default pid = 4528] 

Save Top Command Results

Press (Shift+W) to save the running top command results under /root/.toprc.

Getting Top Command Help

Press ‘h‘ option to obtain the top command help.

 

Exit Top Command After Specific repetition

Top output keep refreshing until you press ‘q‘. With below command top command will automatically exit after 10 number of repetition.

# top -n 10

Executing Unix Top Command in Batch Mode

If you want to execute top command in the batch mode use option -b as shown below.

$ top -b -n 1

Split Top Output into Multiple Panels

To display multiple views of top command output on the terminal, press A. You can cycle through these windows using ‘a’. This is very helpful, when you can sort the output on multiple windows using different top output columns.

Decrease Number of Processes Displayed

Press n in the Interactive mode, which prompts for a number and shows only that.

Maximum tasks = 0, change to (0 is unlimited) 5
...
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 4463 root      20   0 4630856 339436   1340 S  43.9  2.1 161:19.64 java
21067 root      20   0   21956   1140    928 D  22.3  0.0   0:01.29 tar
 2098 vtl       20   0   19396   2640    428 D  19.6  0.0 209:35.16 vtltape
 2104 vtl       20   0   19416   2840    552 D  17.3  0.0 190:25.05 vtltape

Toggle Top Header to Increase Number of Processes Displayed

By default top displays total number process based on the window height. If you like to see additional process you might want to eliminate some of the top header information.

Following is the default header information provided by top.

top - 13:32:27 up 26 days, 23:17,  3 users,  load average: 8.46, 8.70, 8.71
Tasks: 306 total,   2 running, 304 sleeping,   0 stopped,   0 zombie
%Cpu(s):  4.1 us, 11.8 sy,  0.0 ni, 25.0 id, 58.4 wa,  0.0 hi,  0.8 si,  0.0 st
KiB Mem : 16268500 total,   164108 free,  4750740 used, 11353652 buff/cache
KiB Swap:  2097148 total,  2015668 free,    81480 used. 10517748 avail Mem
  • Press l – to hide / show the load average. 1st header line.
  • Press t – to hide / show the CPU states. 2nd and 3rd header line.
  • Press m – to hide / show the memory information. 4th and 5th line.

To sort top output by any column, Press O

Which will display all the possible columns that you can sort by as shown below.

Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return 

  a: PID        = Process Id              v: nDRT       = Dirty Pages count
  d: UID        = User Id                 y: WCHAN      = Sleeping in Function
  e: USER       = User Name               z: Flags      = Task Flags
  ........

When the linux top command is running, Press R, which does the sort in reverse order.