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.

    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.

    1. # top
    2. top - 13:10:13 up 171 days, 20:21, 3 users, load average: 0.01, 0.05, 0.00
    3.  
    4. Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie
    5. Mem: 1034624k total, 1007420k used, 27204k free, 174540k buffers
    6. Swap: 2031608k total, 0k used, 2031608k free, 615904k cached
    7.  
    8. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    9. 11912 apache 15 0 31828 13m 3916 S 1 0.2 0:46.35 httpd
    10. 19299 oracle 19 0 279m 18m 17m S 1 0.2 0:00.03 oracle
    11. 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
    • 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.

    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.

    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?

    1. Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def

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

    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.

    How to view the individual CPUs in the top command?

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