In this article, we’ll explain 3 ways to check CPU utilization in Linux.
CPU utilization refers to a usage of processing resources, or the amount of work handled by a CPU. The performance of the CPU is one of the major determinants of the performance of a system. To ensure the most out of the limited processing power, it’s necessary to monitor how this resource is used.
For this demonstration purpose, we’re using Ubuntu 20.04 server. Following commands can work on any Linux machine.
3 Ways to Check CPU Utilization in Linux
Linux has various built-in system calls to extract performance readings. Some of the tools come pre-installed with all Linux distros, some may require manual installation.
1 – top Command
top command is a most commonly use. The output of the top command divided into two sections. In the first section few lines give a summary of the system resources with CPU utilization and second section display list of processes and threads currently being managed by the Linux kernel. It also offers interactive options to modify its behavior and perform various actions.
The top tool can show CPU utilization. Launch the tool.
List of hotkeys:
- P: Sort processes by CPU usage.
- I: Remove idle processes from the list. To revert, press “I” again.
- M: Sort the list by memory usage.
- S: Sort the list by how long processes have been running.
- U: Filter the processes by the owner.
- K: Kill a process. Requires the PID of the process.
2 – htop Command
htop command allows the user to interactively monitor the system’s vital resources or server’s processes in real time. htop is a newer program compared to top command, and it offers many improvements over top command. The default display of the htop is more comfortable to work with. The UI has better clarity than the top. The texts are colorized and pleasant to look at. It also offers both horizontal and vertical scrolling.
We need to install htop manually. To install htop, use following command:
For Ubuntu based:
# apt install htop -y
For Fedora/CentOS based:
# yum install htop -y
To launch htop use following command:
3 – iostat Command
The iostat command in Linux is used for monitoring system input/output statistics for devices and partitions. It monitors system input/output by observing the time the devices are active in relation to their average transfer rates. Unlike top or htop, iostat doesn’t offer real-time system monitoring.
The iostat tool comes as a part of the sysstat package. It’s available on almost any Linux distro.
To launch iostat use following command:
To check only CPU statistic, use following command:
# iostat -c
Use the flag “-x” for extended statistics. The flag “-t” will define how many times each report should be displayed.
# iostat -xtc 5 2
Some of the commands of iostat:
- iostat: Get report and statistic.
- iostat -x: Show more details statistics information.
- iostat -c: Show only the cpu statistic.
- iostat -d: Display only the device report.
- iostat -xd: Show extended I/O statistic for device only.
- iostat -k: Capture the statistics in kilobytes or megabytes.
- iostat -k 2 3: Display cpu and device statistics with delay.
- iostat -j ID mmcbkl0 sda6 -x -m 2 2: Display persistent device name statistics.
- iostat -p: Display statistics for block devices.
- iostat -N: Display lvm2 statistic information.
We have covered only 3 commands to check CPU utilization. There are more commands and methods to check the same.
In this article, we have seen 3 ways to check CPU utilization in Linux.