Linux Process CPU Usage Monitoring

December 2, 2014 / FAQs

In this tutorial, we will see how to collect CPU usage of a Linux process.

The main purpose behind this query is supervision or debugging. It would be convenient if you know how to retrieve the CPU usage of a particular Linux process. It is often noticed people only check overall CPU usage on a machine. But don’t bother to analyze, which process is consuming more CPU time.

Once they start focusing on which process consumes more CPU. Then it will be helpful for them to optimize that process.

So in this tutorial, we will see how to check the usage of specific Linux processes:

There are several old-school ways to retrieve this value, one indeed wants to retrieve CPU usage by a process at them “T” and the second when retrieving a value called “leveled”. That is to say, that will represent an average CPU usage over its total life.

Here, we will see the two methods which are both correct but can respond to situations and contexts.

Note: here we are going to discuss the recovery of this information statically to the integration of the value in a script or an SNMP response metrology for example.

CPU usage at time T from the command line with the top

The administrative control “top” command, which is present by default on most systems, is very useful. Usually, the “top” command used dynamically. You will see the launch of an array with values that change dynamically based on the activities of each process:

Now, we will look at using the top command statically, particularly you will be asked to show the best of values that can capture over a period of one second, which will be a non-responsive display.

We will also use the optional command “-b” that allows a correct shaping of the output (especially in the case of multiple rows and columns). This is done via the following option to the command “top”. For example:

top –n1 –b

This event will categorize which is to recovered by the name of the search process. For example, if you want to know the CPU consumption of “MySQL”. For example:

top –n1 –b |grep mysql

Result

We will then search to recover only the value of columns for the CPU usage of each process and sub-process. Using the command “cut” to which we will indicate the range of characters to recover on each line. For example:

top –n1 –b |grep mysql |cut –c46-49

This range of characters in the line (here the character from 46 to 49) tuned to your case (the length of the process name specified mainly). With this, we can have several lines (sub-processes), and add the different values. We will also replace commas with “dots” if “bc” doesn’t wish to process the values.

Note: Use “paste –sd+ |sed 's/,/\./g' |bc” command line in front of “top –n1 –b |grep mysql |cut –c46-49” (change values according to your needs)

If the “bc” command is a problem then you must install it; use “apt-get install bc” or “yum install bc”. This is a command to add more values.

The command is particularly interesting here, it helps to make several lines that recovered (just the numbers in the column “CPU” in our case). Option “-s” allows this treatment, and option “d” then specifies a separator to be here “+”, which will give us the final transaction.