Sometimes you may want to know the CPU usage of a particular Linux process. As the CPU usage of a process can fluctuate over its lifetime, you will want to measure the average CPU usage or CPU utilization of the process. For this purpose, a Linux tool set called sysstat may come in handy, which contains a collection of performance monitoring tools for Linux, reporting statistics on disk I/O, CPU, memory, networking, and other system activities. One of the utilities contained in sysstat is pidstat which measures the average CPU usage of Linux processes. In order to use pidstat, you need to install sysstat as follows.
To install sysstat on Debian, Ubuntu or Linux Mint:
To install sysstat on CentOS or RHEL 6.0 and higher:
To use pidstat on CentOS/RHEL 5.* and lower, you need to download sysstat source code since the sysstat package that comes with CentOS 5.* does not contain pidstat.
Once you have installed sysstat, you can use pidstat to measure the average CPU usage of a Linux process as follows.
Linux 22.214.171.124-0.7.1.xs126.96.36.1993.170596xen (cn-nj) 08/17/2012 _i686_ (2 CPU) 02:09:02 PM PID %usr %system %guest %CPU CPU Command 02:09:07 PM 9280 15.40 11.40 0.00 26.80 0 tincd 02:09:12 PM 9280 19.96 6.79 0.00 26.75 0 tincd 02:09:17 PM 9280 17.40 9.20 0.00 26.60 0 tincd . . . .
In the above example, I am measuring the average CPU usage of a Linux process with PID 9280 every five seconds. The "CPU" field next to "%CPU" denotes the CPU processor ID to which the process is attached (in case there are more than one CPU processors).
Note that you need to provide the "interval" parameter (e.g., 5) which specifies the amount of time in seconds between each report. Without the interval parameter, the %CPU reported by pidstat will be the average CPU usage over the period since system startup (boot).
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.