How To Check CPU Usage From Linux Command Line

Hostnbit

Top command to view Linux CPU load

Open a terminal window and enter the following:

top

The system should respond by displaying a list of all the processes that are currently running. It will also give a readout of users, tasks, CPU load, and memory usage.

This list can frequently change, as background tasks start and complete. One helpful switch is to launch top with the –i switch:

top –i

This hides all the idle processes, making it easier to sort through the list.

To quit the top function, press the letter q on your keyboard.

Some other useful commands while top is running include:

  • M – sort task list by memory usage
  • P – sort task list by processor usage
  • N – sort task list by process ID
  • T – sort task list by run time

To get assistance with top, you can press the letter h while it’s running. Or, you can enter the following at a command line:

man top

This will display the manual page for the top command.

mpstat Command To Display CPU Activity

Mpstat is part of a software bundle called sysstat. Most RedHat (and RHEL) based systems included the software package.

For Debian (and Ubuntu) systems, you’ll need to install the sysstat package.

In a terminal window, enter the following:

sudo apt-get install sysstat

Allow the process to complete.

If you’re running an older (4.x or older) version of CentOS or Red Hat derivative, you can use up2date to install sysstat:

sudo up2date install sysstat

For newer (5.x and later) installations of CentOS or Red Hat, sysstat can be installed using the following:

sudo yum install sysstat

Once the process finishes, you can use the mpstat command in the terminal as follows:

mpstat

The system will display usage for each processor (or processor core).

The first line is a set of column labels. The second line is the value for each column:

  • %usr – % CPU usage at the user level
  • %nice – % CPU usage for user processes labeled “nice”
  • %sys – % CPU usage at the system (Linux kernel) level
  • %iowait – % CPU usage idling waiting on a disk read/write
  • %irq – % CPU usage handling hardware interrupts
  • %soft – % CPU usage handing software interrupts
  • %steal – % CPU usage being forced to wait for a hypervisor handling other virtual processors
  • %guest – % CPU usage spent running a virtual processor
  • %idle – % CPU usage on idle time (no processes, and not waiting on a disk read/write)

You can add switches to the mpstat command.

The –P switch allows you to specify a single processor to report:

mpstat –P 0

This would show you a report for the first processor (CPU 0).

mpstat –P ALL

This command would show you the total, like the basic mpstat command. It will also list processes by individual CPU.

The mpstat command only takes a snapshot of CPU usage.

To take a series of snapshots, use a number to indicate an interval and a second number to indicate the number of reports:

mpstat 5 7

This example would generate 7 snapshots, each 5 seconds apart.

sar Command to Show CPU Utilization

The sar tool is a utility for managing system resources.

It’s not limited strictly to CPU usage, but you can use the -u option to track CPU performance.

Use the following command to direct sar to monitor CPU usage at set intervals:

sar –u 5

The –u option tells it to display CPU usage. The 5 indicates that it should display every 5 seconds. This will run indefinitely, to cancel, press Ctrl-C.

iostat Command For Average Usage

In a terminal, enter the following:

iostat

The system will display average CPU usage since the last boot. It will also display input/output load (disk read/write activity).

Other Options To Monitor CPU Performance

Nmon Monitoring Tool

Nmon is a monitoring tool developed by Nigel Griffiths of IBM. To install Nmon on Ubuntu, enter the following:

sudo apt-get install nmon

To install to CentOS, enter the following:

sudo yum epel-release
sudo yum install nmon<

The command to launch nmon is:

Nmon

This will launch the utility, and display all the options. To view CPU usage, press the letter c. To switch back, press c again. For a list of commands, press h. To quit, press q.

Hostnbit

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu Title
Scroll to Top