How to use top command to monitor cpu usage in linux/unix

Top command is a very important monitoring command tool in linux/unix, it can monitor the system running status.

In the last article, we introduced the basic syntax, command options, and some usage examples of the linux top command.

In this article we introduce how to use the top command to monitor cpu usage in linux/unix systems.

Use top command to monitor system CPU usage

In the following example, we use the linux top command to monitor the system CPU usage. We can type top in the terminal and press Enter to see the current CPU usage.

➜  ~ top

Using the linux top monitoring command, we can view the running status of the system in real time, including CPU usage.

The third line of the screenshot shows the current CPU usage in different states.

CPU status field description:

  • us, user :
    time running un-niced user processes
  • sy, system :
    time running kernel processes
  • ni, nice :
    time running niced user processes
  • id, idle :
    time spent in the kernel idle handler
  • wa, IO-wait :
    time waiting for I/O completion
  • hi :
    time spent servicing hardware interrupts
  • si :
    time spent servicing software interrupts
  • st :
    time stolen from this vm by the hypervisor

In the lower list of screenshots, we can view the CPU usage of each command in real time.

Use the top command to monitor the usage of multiple cores of the system CPU

In the above example, we use the top command to monitor the current system operation in real time. In the following example, we will view the multi-core CPU in real time, and the real-time usage of each core.

To view the CPU multi-core usage, we only need to enter the number “1” when using the top command for real-time monitoring.

➜  ~ top

#Enter the number "1"

Use the linux top command sort by cpu usage

In the following example, we will use the linux top monitoring command to sort by CPU usage.

top Sort by CPU in descending order

➜  ~ top -o +%CPU

top Sort by CPU in ascending order

➜  ~ top -o -%CPU

Explanation of top command options:

  • -o
    override-sort-field as: -o fieldname
  • fieldname prepend ‘+
    sorting high to low
  • fieldname prepend ‘
    sorting low to high

Add a Comment

Your email address will not be published. Required fields are marked *