Linux CPU Usage

How to Check Linux CPU Usage or Utilization?

Share:

CPU usage is a significant metric for the Performance of CPU in Linux. On the other hand, Linux is a multi-tasking operating system that distributes the time of each CPU and allows it to do every task via a scheduler. In this article, you will learn how to monitor CPU usage, and you will also understand how to fix high Linux CPU usage. Although there are multiple ways through which you can check the CPU usage. However, the easiest way of doing it is using the top Command.

You have to type ‘top’ in the command prompt and there will be a list of the currently running processes. Moreover, you will also see information about CPU usage. Memory Usage and load average. Press the w key to exit from the top command.

According to IT experts, Top Command is a great tool to measure Linux CPU usage. It offers you a quick insight into how much CPU is utilized by each process. It helps the user to determine the Per CPU usage. Moreover, you will be in a good position to decide how you can reduce the usage. Although the user interface of the top command is not user-friendly. You will see a huge amount of information on this page. Here we will try to cover every important information which can be helpful.

In this first line, you will see (in order): system time (duration since the last restart), the number of active users’ sessions, and the load average of every system. Here the load average is highly relevant for us it puts light on CPU usage of the system over time.

Load Average 

Load average has three given numbers which are 1 5 and 15. Consider these numbers as percentages such as 0.2 means 20% and a load of 1.00 refers to 100%. It is very easy to understand for everyone but you might see averages of more than 1.00. It is because the load average is not an accurate measurement of Linux CPU usage. However, it helps you figure out how much work your system is processing. For instance, a value of 2.50 means that the current load is 250% and demonstrates that the system is overloaded by 150%.

The second line is quite easy to understand which shows the number of tasks currently running and their current situation. There will be a third line where you will find CPU usage along with statistical data. 

Linux CPU Usage From Top

  • us: refers to the time utilized in the user space 
  • sy; Percentage of CPU time utilized in kernel space 
  • ni: indicates the percentage of CPU time spent on current running processes with priority defined by the users 
  • id: percentage of CPU remained idle 
  • wa: percentage spent on waiting for a hard drive to complete reading the available data
  • hi: Time spent a percentage of CPU on hardware processing 
  • si: Time spent processing the software interrupts 

you will see the next two lines are dedicated to the memory information as they are not related to the CPU usage monitoring.

Simplifying Top

Since there is huge information on the top command which means it is not easy to get quick information for anyone. However, top offers us an easy opportunity to get the required details without wasting time by streamlining things. When the top is under process a user can press ‘t’ key to cycle through different views and get an easy outcome of CPU usage

CPU Usage Graph

In case you are finding it difficult to monitor Linux CPU usage through the top you can do it via top. You can use the package manager for the installation 

htop command

As you can see from the screenshot above, the output from htop is more concise and better suited than top  for simple measuring of CPU usage.

You can exit this screen the same way as top, by pressing ‘q’.

There are also other ways through which you can monitor the CPU usage which is mentioned below 

You May Also Like to Read: Best Linux Server Distributions Or Distros of 2022

More Ways To Find Usage 

A few more tools are available for anyone to check the CPU usage which is available in the sysstat package. You need to install it in your system to use all the commands.

After the installation of sysstat you will have total access to mpstat command. Which indicates information similar to the top. However, it will be in an easy-to-understand manner. There is another command available in the package which is sar. 

It is very helpful when combined with a number in the command. It will help you to determine how often the sar command should produce information about CPU usage

For example, to check CPU usage every 4 seconds:

$ sar 4

Graphical Monitoring 

In case you are using a desktop client or a graphical user interface. There will be graphical tools for keeping a check on the Linux CPU usage. If we talk about UBUNTU, it has Gnome by default and the command required to initiate the system manager is 

$ gnome-system-monitor

After giving the command a window will open in front of you resembling with window’s task manager. Where you can easily specify the processors by their usage. On the other hand, some other distributions might have the same tool for the purpose.

How to Configure Alerts for Monitoring

An individual can use multiple ways to code a script that can easily measure the usage of the CPU. Here we will explore some possible ways which can help you track down the CPU usage every second. It will inform you by email when the usage of the CPU is higher than usual.

The Script uses sad to find out the average Idle CPU Percentage from sar. In addition to that, it uses and if function to check if the percentage is below a certain level. If it is low then the administrator will receive an email in the form of intimation regarding the situation. In case the CPU configuration is 20% and the usage is more than 70% user will get an email instantly.

You can adjust the script as per your own requirements for instance if you want it to send you a warning alert to record a log fine instead of sending an email you can do it by calling the script from cron.

In case there are issues with Linux CPU usage you don’t need to worry at all. As we are here to help you fix the problem without asking for further assistance.

How to high Linux CPU Usage

You need to do a few things in case you find your system is showing a high CPU usage for a couple of days.

  • Run the command prompt and type ‘top’ and hit enter. You will see the current processes running on your system
  • In case you see a certain Process utilizing a lot of your CPU you can stop it through a command of kill <pid>. Here kill <pid> is the actual process id which you want to stop quickly
  • If you want to find the source of high CPU usage you need to run the command prompt and type journalctl -xe. The command will display all the logs of your computer 
  • Here you can sort out the entries which contain the word ‘cpu’ by using the “-e” parameter.
  • If the results are not fruitful despite following the above steps and you are not getting the root cause of the problem. You can diagnose the issue by typing the command lsof.
  • The “lsof” tool can be used to show information about open files and network connections.
  •  If you’re still having trouble, you can try to find more information about your problem by searching the internet or posting a question on a forum.

For Special discounts and offers, visit our official Facebook Page.