Cpustat – Monitors CPU Utilization by Running Processes in Linux

Cpustat is a command-line utility for monitoring CPU utilization by running processes in Linux. It allows users to view the amount of CPU time being used by individual processes and groups of processes, providing insights into how system resources are being utilized. With Cpustat, users can identify processes that are consuming too much CPU time and take action to optimize system performance.

Installing Cpustat

Cpustat is available for most Linux distributions and can be installed using the package manager. On Debian/Ubuntu, use the following command to install

sudo apt-get install cpustat

On Fedora/CentOS, use the following command

sudo dnf install cpustat

Basic Usage

To use Cpustat, simply run the command cpustat followed by any optional parameters. By default, Cpustat will display a summary of CPU utilization by all running processes.

cpustat

The output provides a summary of CPU utilization by all running processes, including the total number of processes, the total amount of CPU time, and the percentage of CPU time used by each process.

Command Options

Monitor Specific Processes

To display CPU utilization for a specific process, use the -p option followed by the process ID

cpustat -p PID

To monitor a group of processes, use the -g option followed by a process group name

cpustat -g group_name

Display Interval

To change the display interval, use the -i option followed by the number of seconds between updates

cpustat -i 2

This will update the display every 2 seconds.

Sorting

To sort output by different criteria, use the -S option followed by the desired criteria

cpustat -S %cpu

This will sort output by CPU usage percentage.

Output Format

To select the format of the output, use the -o option followed by the desired format

cpustat -o csv

This will output data in comma-separated value format for easier analysis.

Scripting with Cpustat

One of the most powerful features of Cpustat is its ability to be scripted and automated. By using Cpustat in conjunction with shell scripts, you can create custom monitoring solutions tailored to your specific needs.

CPU Utilization Over Time

To monitor CPU utilization over time, you can create a shell script that runs Cpustat at regular intervals and appends the output to a log file

#!/bin/bash

while true; do
   cpustat -o csv >> cpu_log.csv
   sleep 1
done

This script continuously runs Cpustat every second and appends the output to a CSV file named cpu_log.csv.

High CPU Usage Alerts

To create an alerting script that monitors CPU usage and sends an alert when certain thresholds are exceeded

#!/bin/bash

while true; do
   cpustat -o csv | awk -F ',' '{ if ($2 > 50) print "High CPU usage detected: " $0 }'
   sleep 1
done

This script uses awk to parse the CSV output and compare CPU usage against a threshold of 50%.

Process Profiling

To profile specific processes, you can monitor individual applications separately

#!/bin/bash

while true; do
   cpustat -o csv -p nginx >> nginx_cpu.csv
   cpustat -o csv -p postgres >> postgres_cpu.csv
   sleep 1
done

This script monitors nginx and postgres processes separately, writing their CPU usage data to individual log files.

Comparison with Other Tools

Feature Cpustat top htop
Process-level monitoring Yes Limited Yes
Customizable output Yes (CSV, JSON) Limited Moderate
Real-time monitoring Yes Yes Yes
Scripting support Excellent Basic Limited

Key Advantages

  • Process-level monitoring Unlike basic system tools, Cpustat can monitor CPU utilization by individual processes or groups, making it easier to identify resource-intensive applications.

  • Flexible output formats Supports multiple output formats including CSV and JSON for easy integration with analysis tools and scripts.

  • Real-time monitoring Provides continuous monitoring capabilities to track CPU usage changes over time and identify performance spikes.

  • Automation-friendly Designed for scripting and automation, making it ideal for creating custom monitoring solutions and alerts.

Conclusion

Cpustat is a powerful and flexible tool for monitoring CPU utilization by running processes in Linux. Its process-level monitoring capabilities, customizable output formats, and excellent scripting support make it an essential tool for system administrators and developers who need detailed CPU performance insights.

Updated on: 2026-03-17T09:01:38+05:30

937 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements