Quantcast
Channel: GoLinuxHub
Viewing all articles
Browse latest Browse all 392

8 examples to help you understand top command usage in Unix/Linux

$
0
0
top is one of my favorite tool for monitoring system usage and also to make any change for improving system
performance.

Let me show you some examples on the usage of top command

Intro:
The  top  program  provides a dynamic real-time view of a running system.  It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.  The types of system summary information shown and the types, order and size of information displayed for tasks are all user configurable and that  configuration can be made persistent across restarts.

1. Without any arguments

[deepak@server1 ~]$ top
top - 17:51:07 up 1 day,  2:56, 27 users,  load average: 5.33, 29.71, 28.33
Tasks: 1470 total,   1 running, 1469 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  264114424k total, 253006956k used, 11107468k free,    66964k buffers
Swap: 33554424k total,     3260k used, 33551164k free, 245826024k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1960 deepak  15   0 30452 3220 1540 R  2.3  0.0   0:00.78 top
 2457 root      11  -5     0    0    0 S  2.3  0.0  11:36.93 kacpid
 2493 pmartprd  16   0 1397m 289m 9.8m S  0.3  0.1  18:36.07 pmrepagent
 4639 pmartprd  15   0  787m  54m 4080 S  0.3  0.0   5:19.55 pmserver
14402 root      RT   0  151m 5256 2872 S  0.3  0.0   1:41.40 multipathd
17886 root      10  -5     0    0    0 S  0.3  0.0   0:07.41 kondemand/11

Generally I use top without any arguments, but the magic is mostly done from the top command line which must of us skip. Well before taking you to that part let me explain you the various system related features which are shown by top command

NOTE: You can enable or disable the marked blue line by pressing "l" once top is running
top - 17:51:07 up 1 day,  2:56, 27 users,  load average: 5.33, 29.71, 28.33
Tasks: 1470 total,   1 running, 1469 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  264114424k total, 253006956k used, 11107468k free,    66964k buffers
Swap: 33554424k total,     3260k used, 33551164k free, 245826024k cached
Explanation: This line tells you about the uptime of your system along with load average value.

NOTE: You can enable/disable the marked blue line by pressing "t"
top - 17:51:07 up 1 day,  2:56, 27 users,  load average: 5.33, 29.71, 28.33
Tasks: 1470 total,   1 running, 1469 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:  264114424k total, 253006956k used, 11107468k free,    66964k buffers
Swap: 33554424k total,     3260k used, 33551164k free, 245826024k cached
Explanation:  This line gives us a brief detail of all the tasks running/sleeping/stopped currently in the system along with the CPU Usage

NOTE: You can enable/disable the marked blue line by pressing "m"
top - 17:51:07 up 1 day,  2:56, 27 users,  load average: 5.33, 29.71, 28.33
Tasks: 1470 total,   1 running, 1469 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  264114424k total, 253006956k used, 11107468k free,    66964k buffers
Swap: 33554424k total,     3260k used, 33551164k free, 245826024k cached
Explanation:  The next line shows your memory(RAM and swap) usage and capacity.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
13916 stmprd    18   0  903m 129m 9936 S 51.4  0.1   3:07.01 java
13921 stmprd    18   0  901m 128m 9936 S 49.8  0.0   3:02.92 java
13825 stmprd    18   0  951m 190m 9932 S 49.5  0.1   3:07.13 java
13856 stmprd    20   0  978m 197m 9936 S 49.2  0.1   3:05.89 java
13853 stmprd    18   0  921m 150m 9932 S 48.5  0.1   3:09.14 java
13875 stmprd    18   0  907m 132m 9940 S 48.5  0.1   3:09.49 java
13937 stmprd    25   0  926m 165m 9936 S 48.2  0.1   3:10.31 java
13919 stmprd    18   0  917m 153m 9936 S 47.5  0.1   3:05.92 java
13879 stmprd    25   0  921m 160m 9936 S 47.2  0.1   3:08.43 java
13908 stmprd    25   0  901m 131m 9932 S 47.2  0.1   3:12.23 java
13905 stmprd    25   0  907m 137m 9932 S 46.6  0.1   2:59.85 java


The left sections shows you the details of the process running along with the below details
Fields/Column
Description
PID
Process Id
USER
The effective user name of the task's owner
PR
The priority of the task
NI
The nice value of the task.  A negative nice value means higher priority, whereas a positive nice value means lower priority.  Zero in this field simply means priority will not be adjusted in determining a task's dispatchability
%CPU
The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
%MEM
A task's currently used share of available physical memory
TIME+
Total CPU time the task has used since it started
S
The status of the task which can be one of:
             'D' = uninterruptible sleep
             'R' = running
             'S' = sleeping
             'T' = traced or stopped
             'Z' = zombie
RES
The non-swapped physical memory a task has used
SHR
The amount of shared memory used by a task
Command
Display the command line used to start a task or the name of the associated program

2. Arrange Tasks with High to Low CPU Usage


Press "P" or "shift+p" once top is running to arrange all the tasks with High to Low CPU Usage as shown below
top - 18:03:00 up 1 day,  3:08, 27 users,  load average: 12.54, 32.34, 32.75
Tasks: 1485 total,   3 running, 1482 sleeping,   0 stopped,   0 zombie
Cpu(s): 41.2%us,  0.8%sy,  0.0%ni, 56.6%id,  1.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  264114424k total, 258863028k used,  5251396k free,    76308k buffers
Swap: 33554424k total,     3256k used, 33551168k free, 250950544k cached

  PID USER      PR  NI  VIRT  RES  SHR S
%CPU %MEM    TIME+  COMMAND
 9663 stmprd    22   0  902m 301m 9888 S 2578.3 0.1   2:27.04 java
32117 etlprd    18  -1 32416 5908 1716 R  6.2 0.0   0:04.84 cleanup_dirfile
10053 root      18  -1 27100 1936 1460 S  4.9 0.0   0:00.15 ps
 5456 pmartprd  16   0 1182m 130m 8560 S  3.9 0.1  38:39.72 pmserver
17492 deepak  16   0 30592 3388 1544 R  3.6 0.0   0:17.11 top
 2843 pmartprd  15   0  730m  48m 4052 S  3.3 0.0   4:40.33 pmserver
 2457 root      11  -5     0    0    0 S  2.9 0.0  11:42.39 kacpid
 3731 tdmsprd   15   0  370m  49m  32m S  2.3 0.0   0:00.64 pmdtm.orig
 

3. Arrange Tasks with High to Low Memory Usage


Press "M" or "shift+m"once top is running to arrange all the tasks with High to Low Memory Usage as shown below
top - 18:04:26 up 1 day,  3:09, 27 users,  load average: 37.12, 34.56, 33.44
Tasks: 1676 total,   1 running, 1675 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.3%us, 76.7%sy,  0.0%ni, 19.7%id,  1.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  264114424k total, 262605184k used,  1509240k free,    77924k buffers
Swap: 33554424k total,     3256k used, 33551168k free, 252198368k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU
%MEM   TIME+  COMMAND
 1852 pmartprd  18   0 2005m 319m 4776 S  6.9  4.1 28:34.32 java
 2493 pmartprd  16   0 1397m 289m 9.8m S  0.0  4.0 18:37.79 pmrepagent
20557 etlprd    15   0  911m 201m 3024 S  0.0  3.0 17:09.02 pmdtm.orig
18778 root      RT   0  286m 188m 156m S  0.0  2.1 13:24.98 aisexec
 5456 pmartprd  15   0 1182m 130m 8560 S  6.2  1.1 38:40.58 pmserver
16004 etlprd    14  -1  179m  83m 2636 S  0.0  0.1  9:41.36 db2bp
11272 stmprd    25   0  906m  67m 9736 S 99.7  0.0  0:48.11 java

4. Change the nice value (priority) of any task

To understand what is nice value follow the below link
Change priority of process using renice

Press "r" when top is running on the terminal. You should get a prompt as shown below in blue color
top - 18:08:38 up 115 days,  8:44,  4 users,  load average: 0.03, 0.03, 0.00
Tasks: 325 total,   2 running, 323 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  6.4%sy,  0.0%ni, 93.3%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063848k used, 47368880k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297572k cached
PID to renice:1308 [Hit Enter]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      39  19     0    0    0 R 100.1  0.0   9431:35 kipmi0
 1308 deepak  16   0 29492 2292 1512 S  0.7  0.0   0:00.33 top
 6116 root      15   0  369m  30m  11m S  0.7  0.1  77:24.97 cimserver

Give the PID whose nice value has to be changed and hit "Enter". Then give the nice value for the PID
top - 18:08:38 up 115 days,  8:44,  4 users,  load average: 0.03, 0.03, 0.00
Tasks: 325 total,   2 running, 323 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  6.4%sy,  0.0%ni, 93.3%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063848k used, 47368880k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297572k cached
Renice PID 1308 to value:-1 [Hit Enter]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      39  19     0    0    0 R 100.1  0.0   9431:35 kipmi0
 1308 deepak  16   0 29492 2292 1512 S  0.7  0.0   0:00.33 top
 6116 root      15   0  369m  30m  11m S  0.7  0.1  77:24.97 cimserver

Verify the changes
top - 18:09:06 up 115 days,  8:45,  4 users,  load average: 0.13, 0.06, 0.01
Tasks: 325 total,   1 running, 324 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063276k used, 47369452k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297588k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 
1308 deepak  15  -1 29492 2292 1512 S  0.7  0.0   0:00.42 top
 5359 root      34  19     0    0    0 S  0.7  0.0   9431:42 kipmi0
    1 root      15   0 10352  692  580 S  0.0  0.0   0:02.16 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:02.37 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/

5. Kill any task

Press "k" on the terminal when top is running. You should get a prompt as shown below in blue color
top - 18:09:31 up 115 days,  8:45,  4 users,  load average: 0.08, 0.05, 0.01
Tasks: 325 total,   1 running, 324 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.1%sy,  0.0%ni, 99.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2062036k used, 47370692k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297596k cached
PID to kill:1308
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      34  19     0    0    0 S  1.3  0.0   9431:42 kipmi0
 6460 root      24   0  179m  30m 3976 S  1.0  0.1  79:04.77 java
 1308 deepak  15  -1 29492 2292 1512 S  0.7  0.0   0:00.49 top
 1434 root      15   0 29492 2288 1516 R  0.7  0.0   0:00.13 top

top - 18:09:31 up 115 days,  8:45,  4 users,  load average: 0.08, 0.05, 0.01
Tasks: 325 total,   1 running, 324 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.1%sy,  0.0%ni, 99.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2062036k used, 47370692k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297596k cached
Kill PID 1308 with signal [15]: [Hit Enter for default]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      34  19     0    0    0 S  1.3  0.0   9431:42 kipmi0
 6460 root      24   0  179m  30m 3976 S  1.0  0.1  79:04.77 java
 1308 deepak  15  -1 29492 2292 1512 S  0.7  0.0   0:00.49 top

6. View all the processes running by a user

Press "u" on the terminal when top is running. You should get a prompt as shown below in blue color
top - 18:12:24 up 115 days,  8:48,  4 users,  load average: 0.06, 0.05, 0.00
Tasks: 328 total,   1 running, 327 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.4%sy,  0.0%ni, 99.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063268k used, 47369460k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297660k cached
Which user (blank for all):deepak [Hit Enter]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1729 root      15   0 29488 2196 1432 R  2.0  0.0   0:00.01 top
    1 root      15   0 10352  692  580 S  0.0  0.0   0:02.16 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:02.37 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0

top - 18:12:41 up 115 days,  8:48,  4 users,  load average: 0.04, 0.05, 0.00
Tasks: 328 total,   1 running, 327 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2062356k used, 47370372k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297672k cached

  PID USER     PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1561 deepak  17   0  3984  780  468 S  0.0  0.0   0:00.00 man
 1564 deepak  19   0  8704  964  816 S  0.0  0.0   0:00.00 sh
 1566 deepak  23   0  8704  464  316 S  0.0  0.0   0:00.00 sh
 1571 deepak  16   0  8452  892  712 S  0.0  0.0   0:00.01 less
31328 deepak  15   0  110m 2348 1264 S  0.0  0.0   0:00.20 sshd
31329 deepak  16   0 27676 2564 1816 S  0.0  0.0   0:00.02 bash
31422 deepak  15   0  109m 2360 1260 S  0.0  0.0   0:00.14 sshd
31423 deepak  15   0 27548 2500 1784 S  0.0  0.0   0:00.02 bash

7. Change delay between terminal refresh

By default the top terminal is set for auto refresh after every 3 seconds but if you want you can change it as per your requirement
Press "d" when top is running. You should get a prompt as shown below in blue color
top - 18:14:55 up 115 days,  8:50,  4 users,  load average: 0.01, 0.04, 0.00
Tasks: 328 total,   1 running, 327 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063828k used, 47368900k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297728k cached
Change delay from 3.0 to:2.0 [Hit Enter]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      34  19     0    0    0 S  0.7  0.0   9431:58 kipmi0
 1795 root      15   0 29492 2300 1524 R  0.3  0.0   0:00.20 top
    1 root      15   0 10352  692  580 S  0.0  0.0   0:02.16 init

Verify the changes. You must see the screen buffer getting refresh much earlier or just to verify you can provide a higher value of delay and observer the refresh rate on the terminal

8. No. of task to be displayed

By default this option is set to unlimited that is the reason your terminal is fully covered with list of tasks when you run the top command. Any how you can list the no of tasks to be visible once you run top command

Press "n"when top is running. You should get a prompt as shown below in blue color
top - 18:18:07 up 115 days,  8:54,  4 users,  load average: 0.01, 0.03, 0.00
Tasks: 328 total,   1 running, 327 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.2%sy,  0.0%ni, 99.7%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2063348k used, 47369380k free,   310072k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1297804k cached
Maximum tasks = 0, change to (0 is unlimited): 2 [Hit Enter]
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      34  19     0    0    0 S  2.3  0.0   9432:08 kipmi0
 1795 root      15   0 29492 2304 1528 R  0.7  0.0   0:00.65 top
    1 root      15   0 10352  692  580 S  0.0  0.0   0:02.16 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:02.37 migration/0

top - 14:48:40 up 116 days,  5:24,  3 users,  load average: 0.05, 0.04, 0.00
Tasks: 318 total,   1 running, 317 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  49432728k total,  2051952k used, 47380776k free,   310176k buffers
Swap:  2097144k total,        0k used,  2097144k free,  1293800k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5359 root      34  19     0    0    0 S  1.0  0.0   9502:15 kipmi0
25009 prasadee  15   0 29492 2280 1516 R  0.3  0.0   0:01.88 top

I hope the tutorial was useful.

References:
top man page for Linux

Related articles
Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
7 Commands to read or view the contents of a file using CLI in Linux


Viewing all articles
Browse latest Browse all 392

Trending Articles