Hack 90. Top Command Examples

top command displays real time information about various performance metrics of the system such as CPU Load, Memory Usage, Processes list etc.

Syntax: top [options]

How to view my current system status including CPU usage?

Execute top without any option from the command line, which will display the output shown below. The top command output will keep displaying the real-time values, until you press “Control + c” or q to exit from the command output.

# top
top - 13:10:13 up 171 days, 20:21,  3 users,  load average: 0.01, 0.05, 0.00

Tasks: 194 total,   1 running, 193 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.6% us,  0.7% sy,  0.0% ni, 98.7% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:   1034624k total,  1007420k used,       27204k free,   174540k buffers
Swap:  2031608k total,               0k used,  2031608k free,    615904k cached

PID   USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11912 apache    15     0 31828  13m  3916 S          1         0.2   0:46.35   httpd
19299  oracle    19     0  279m  18m   17m S          1         0.2   0:00.03   oracle
11398 jsmith     16     0  107m  28m  6404 S          0         0.4   0:03.07   perl

How to read the output of the top command shown above?

  • Line 1 “top”, indicates that the system has been up and running for 171 days.
  • Line 2 “Tasks”, displays the total number of processes along with a breakdown of running, sleeping, stopped and zombie processes count.
  • Line 3 “Cpu(s)” displays the current CPU utilization of the system. In this example, CPU is 98.7% idle
  • Line 4 “Mem” and line 5 “Swap” provides the memory information. This is the same information from the free command.
  • The rest of the lines display all the active processes on the system, sorted default by CPU usage (%CPU column). i.e the most CPU intensive processes will be displayed on the top by default.

There are several command line options and interactive options available for top commands. Let us review couple of essential options for top command.

How to identify the most memory intensive processes?

While the output of the top command displayed, press F, which will display the following message and show all fields available for sorting, press n (which is for sorting the processes by Memory) and press enter. This will display the processes in the top output sorted by memory usage.

Current Sort Field:  K  for window 1:Def

Select sort field via field letter, type any other key to return

How to add additional fields (for e.g. CPU Time) to the top output?

While the top command is running, press f, which will display the following message and show all fields available for display, press l, which will add the CPU Time to the display columns in the top output.

Current Fields:  AEHIOQTWKNMbcdfgjplrsuvyzX  for window 1:Def

Toggle fields via field letter, type any other key to return

How to get the full path name and parameters of the running processes?

While the top command is running, press c, which will display full pathname of running processes as shown below in the command column. i.e Instead of httpd, it displays /usr/local/apache2/bin/httpd.

PID   USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11912 apache    15     0 31828  13m  3916 S          1         0.2   0:46.35   /usr/local/apache2/bin/httpd

How to view the individual CPUs in the top command?

While the top command is running, press 1 (number one), which will display the performance data of the individual CPUs on that machine as shown below.

top - 13:10:13 up 171 days, 20:21,  3 users,  load average: 0.01, 0.05, 0.00
Tasks: 194 total,   1 running, 193 sleeping,   0 stopped,   0 zombie
Cpu0  : 10.2% us,  2.6% sy,  0.0% ni, 86.8% id,  0.3% wa,  0.0% hi,  0.0% si
Cpu1  :  9.6% us,  8.0% sy,  0.0% ni, 82.4% id,  0.0% wa,  0.0% hi,  0.0% si
Cpu2  :  1.3% us,  1.3% sy,  0.0% ni, 95.0% id,  2.3% wa,  0.0% hi,  0.0% si
Cpu3  :  0.0% us,  0.0% sy,  0.0% ni, 100.0% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:   1034624k total,  1007420k used,       27204k free,   174540k buffers
Swap:  2031608k total,               0k used,  2031608k free,    615904k cached