Linux process CPU usage monitoring

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 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 usage of specific Linux process:

There are several old school ways to retrieve this value, one can indeed want to retrieve CPU usage by a process at them “T” and second when retrieving a value that can be 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 an example.

CPU usage at time T from the command line with top

The administrative control “top” command which is present by default on the most of systems is very useful. Usually, “top” command is 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 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 optional command “-b” that allows a correct shaping of the output (especially in the case of multiple rows and columns). This can be done via the following option to the command “top”. For example:

top –n1 –b

This event will categorize which is to be 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


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

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

This range of character in the line (here the character from 46 to 49) can be tune to your case (the length of the process name specified mainly). With this, we can then have several lines (sub-processes), 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 help to make several lines that are recovered (just the numbers in the column “CPU” in our case), the option “-s” allows this treatment, option “d” then to specify a separator to be here “+”, which will give us the final transaction.