AMP AMP

top Command in Linux with Examples

top Command

Top command is used to monitor the current running process of Linux/Unix based system in real time. It displays the process, the memory being used and other information.


Top command with no options

Top command with no options will show information like tasks, memory, CPU, users, load average and swap. Press ‘ q’ to quit the top command output.

Example

[root@linuxhelp]# top
top - 05:02:31 up 3:23, 2 users, load average: 0.16, 0.34, 0.30
Tasks: 236 total, 1 running, 235 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012548k total, 928680k used, 83868k free, 69076k buffers
Swap: 2097144k total, 107828k used, 1989316k free, 491248k cached

PID  USER PR NI VIRT RES  SHR  S %CPU %MEM TIME+    COMMAND
2584 root 20 0  167m 31m  4956 S 3.3  3.1  17:22.76 Xorg
4050 root 20 0  301m 6268 4184 S 0.7  0.6  0:04.76  gnome-terminal


Highlight Running Process in Top

' z' option is used to highlight the running process in top. The running processes will be displayed in a different color to identify it.

Example

top - 05:05:13 up 3:25, 2 users, load average: 0.09, 0.23, 0.26
Tasks: 236 total, 1 running, 235 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012548k total, 931332k used, 81216k free, 69328k buffers
Swap: 2097144k total, 107436k used, 1989708k free, 492504k cached
PID  USER  PR NI VIRT  RES  SHR  S  %CPU %MEM   TIME+     COMMAND
2584 root  20 0  167m  31m  4988 S  1.7   3.2   17:24.48  Xorg
4050 root  20 0  302m  6552 4316 S  0.7   0.6   0:04.98   gnome-terminal
41   root  20 0  0        0    0 S  0.3   0.0   0:04.58   ata/0
42   root  20 0  0        0    0 S  0.3   0.0   0:03.29   ata/1


Absolute Path of Processes

To view the absolute path of running process use the option ’ c’ .

Example

2584 root 20 0 167m  31m  4988 S 1.3 3.2  17:25.71  /usr/bin/Xorg :0 -n
4050 root 20 0 302m  6588 4320 S 0.3 0.7  0:05.14   /usr/bin/gnome-term
5820 root 20 0 1119m 153m 60m  S 0.3 15.5 0:07.16  /usr/lib64/libreoff


Modify ‘ Screen Refresh Interval’ in Top

The screen refresh interval is 3.0 seconds by default, to change the screen refresh interval use ‘ d‘ option and change it as desired.

Example

Change delay from 3.0 to: 2


Kill running process

To kill a running process first find the PID of process and press ' k' option then it ask for the pid to kill, enter the pid of the particular running process to kill.

Example

 PID to kill: 3172


Sort by CPU Utilization

To view the processes with high CPU utilization press ‘ shift + p’ , now the processes is sorted with high CPU utilization from the top.

Example

PID  USER PR NI VIRT  RES  SHR  S %CPU %MEM TIME+    CODE DATA COMMAND
2321 root 20 0  170m  45m  8256 S 5.3  4.6  2:25.4.4 2100 38m  Xorg
4191 root 20 0  293m  14m  9900 S 1.3  1.5  0:12.63  300  15m  gnome-ter
2632 root 20 0  40336 592  336  S 0.3  0.1  0:11.47  240  324  udisks-da
4249 root 20 0  60244 3652 2808 S 0.3  0.4  0:01.01  360  868  ssh


Sort by Memory utilization

To view the processes with high memory utilization press ‘ shift + m’ ,now the processes is sorted with high memory utilization from the top.

Example

PID  USER PR NI VIRT  RES  SHR  S  %CPU  %MEM TIME+   CODE DATA COMMAND
5037 root 20 0  1150m 102m 62m  S  0.0   10.4 0:14.26    8 553m soffice.b
2321 root 20 0  170m  45m  8256 S  5.3   4.6  2:26.41 2100 38m  Xorg
2616 root 20 0  929m  25m  12m  S  0.0   2.6  0:22.18 1764 553m nautilus
2690 root 20 0  555m  21m  11m  S  0.0   2.2  0:12.47  172 157m clock-app
2894 root 20 0  45228 21m  496  S  0.0   2.1  0:00.12   28 20m  restoreco
2676 root 20 0  313m  17m  7816 S  0.0   1.7  0:01.00    4 9148 python


Change the Priority of a task

To change the priority of a task press ‘ r’ when top command is running on the terminal.it asks for PID for changing the priority, enter the PID and press enter.

Changing priority for Firefox

Before Changing:

PID  USER PR NI VIRT RES  SHR  S %CPU %MEM TIME+    COMMAND
7691 root 20 0  813m 118m 30m  S 0.3  12.0 0:17.76  firefox
1    root 20 0 19352 1540 1228 S 0.0  0.2   0:01.51 init
2    root 20 0     0    0    0 S 0.0  0.0   0:00.00 kthreadd
3    root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
PID to renice:7691
Renice PID 7691 to value:1

After Changing:

PID  USER PR NI VIRT  RES   SHR  S %CPU %MEM  TIME+   COMMAND
7691 root 21 1  813m  118m  30m  S 0.3  12.0  0:17.76 firefox
1    root 20 0  19352 1540  1228 S 0.0  0.2   0:01.51 init
2    root 20 0      0    0     0 S 0.0  0.0   0:00.00 kthreadd
3    root RT 0      0    0     0 S 0.0  0.0   0:00.00 migration/0


Displaying No. of Tasks

By default, displaying tasks in top command is set to unlimited, to change the displaying no. of tasks press ‘ n’ and enter the no. of tasks to be displayed.

Example

Before Changing

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
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

After Changing

top - 07:50:47 up 6:17, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.8%us, 0.3%sy, 0.0%ni, 94.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012548k total, 851872k used, 160676k free, 44276k buffers
Swap: 2047992k total, 0k used, 2047992k free, 368016k cached
PID  USER PR NI VIRT RES SHR  S %CPU %MEM TIME+   COMMAND
2307 root 20 0  165m 41m 8576 S 4.0  4.2  0:32.44 Xorg
5245 root 20 0  452m 18m 10m  S 1.7  1.8  0:04.28 gnome-terminal


Adding New Field

To add new field in top output, press ‘ f’ , then it will list available fields in top command and you can add the field that you need.

Example

To add code size field press ‘ r’

PID  USER PR NI VIRT RES SHR  S %CPU  %MEM TIME+   CODE  DATA  COMMAND
2307 root 20 0  165m 41m 8592 S 1.7   4.2  0:34.91 2100  34m   Xorg
5245 root 20 0  452m 18m 10m  S 0.7   1.8  0:04.98 300   173m  gnome-ter
7    root 20 0     0   0   0  S 0.3   0.0  0:25.94 0     0     events/0
2618 root 20 0  412m 18m 13m  S 0.3   1.9  0:22.88 596   152m  vmtoolsd

Now the code field is added to the top output


To View a Specific User Process

To view a specific User process details use the option ‘ u’ with the top command mentioning the user name.

Example

[root@linuxhelp]# top -u user1
top - 05:04:16 up 3:24, 2 users, load average: 0.07, 0.26, 0.27
Tasks: 236 total, 1 running, 235 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.1%hi, 0.0%si, 0.0%st
Mem: 1012548k total, 931056k used, 81492k free, 69240k buffers
Swap: 2097144k total, 107480k used, 1989664k free, 492400k cached
2321 root 20 0 170m  45m 8300 S 4.3  4.6  2:29.96 2100 38m  Xorg
4191 root 20 0 293m  14m 9900 S 1.3  1.5  0:13.90 300  15m  gnome-ter
7    root 20 0    0   0    0  S 0.3  0.0  0:35.07   0   0   events/0


Save Top Command Results

To save the top command results, press Shift+w and now the result is saved to the /root/.toprc file

Example

RCfile for " top with windows"  # shameless braggin' 
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0
Def fieldscur=AEHIOQTWKNMbcdfgjplRSuvyzX
winflags=62777, sortindx=10, maxtasks=0
summclr=1, msgsclr=1, headclr=3, taskclr=1
Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
winflags=62777, sortindx=0, maxtasks=0
summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
winflags=62777, sortindx=13, maxtasks=0
summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX
winflags=62777, sortindx=4, maxtasks=0
summclr=3, msgsclr=3, headclr=2, taskclr=3
~
~
~
" ~/.toprc"  14L, 617C 14,2-9 All


Refresh Top Command output continuously

Press ‘ a’ to refresh the top command output continuously.


Display Top output in multiple windows

To display the top command output in multiple windows press ‘ A’ . To switch between the multiple windows press ‘ a’ .

Example

1:Def - 07:57:14 up 10:04, 5 users, load average: 0.00, 0.00, 0.00
Tasks: 157 total, 1 running, 156 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.8%us, 0.7%sy, 0.0%ni, 94.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1012548k total, 721412k used, 291136k free, 46924k buffers
Swap: 2047992k total, 3140k used, 2044852k free, 286920k cached

PID  USER  PR NI VIRT RES SHR  S %CPU %MEM  TIME+   CODE  DATA  COMMAND
2321 root  20 0  170m 45m 8300 S 4.6  4.6   2:36.33 2100  38m   Xorg
4191 root  20 0  293m 14m 9900 S 1.3  1.5   0:16.50 300   15m   gnome-te
2616 root  20 0  929m 25m 12m  S 0.3  2.6   0:22.27 1764  553m  nautilus

PID   PPID TIME+   %CPU %MEM PR NI S VIRT SWAP  RES  UID COMMAND
10413 9129 0:00.11 0.3  0.1  20 0  R 15032  0   1268  0   top
9129  4191 0:00.03 0.0  0.2  20 0  S 105m   0   1924  0   bash
7007  4193 0:00.07 0.0  0.3  20 0  S 140m   0   3428  0   vim

PID  %MEM VIRT  SWAP  RES  CODE  DATA  SHR  nFLT nDRT S  PR  NI %CPU COMMAND
5037 10.4 1150m    0  102m 8     553m  62m  150  0    S  20  0  0.0  soffice.bin
2321 4.6  170m     0  45m  2100  38m   8300 11   0    S  20  0  4.6  Xorg
2616 2.6  929m     0  25m  1764  553m  12m  138  0    S  20  0  0.3  nautilus
2690 2.2  555m     0  21m  172   157m  11m  75   0    S  20  0  0.0  clock-applet

PID   PPID UID  USER    RUSER   TTY TIME+   %CPU %MEM S COMMAND
2470  1    499  rtkit   rtkit   ?   0:00.44 0.0  0.1  S rtkit-daemon
1838  1    29   rpcuser rpcuser ?   0:00.03 0.0  0.1  S rpc.statd
1790  1    32   rpc     rpc     ?   0:00.08 0.0  0.1  S rpcbind
5037  5026 0    root    root    ?   0:14.87 0.0  10.4 S soffice.bin

Save

Tag : Top command
FAQ
Q
How to view a specific user information?
A
To view a specific User process details use the option &lsquo u&rsquo with the top command mentioning the username:
# top -u user1
Q
How to scroll the top command?
A
"top -b -n 1" gives a static print out of the top command to the scrollable termnial
Q
How to export the output of 'top' command on hourly basis?
A
Use the following command "top >> monitor.log" & create a cron for every I hour
Q
What is the command to get zombie process?
A
You can use the following command "ps -el | grep Z"
Q
How to track specific process using top command?
A
Use "top -pXXXX xxxx" which will indicates the pid of process