Command To Find Cpu Usage In Linux

4 min read

Introduction

Finding the command to find cpu usage in linux is a fundamental skill for system administrators, developers, and anyone interested in performance tuning. Whether you are troubleshooting a slow server, monitoring resource consumption, or simply curious about how your machine is performing, Linux provides a variety of built‑in tools that can display CPU utilization in real time or via historical logs. This article walks you through the most reliable commands, explains how CPU usage is calculated, and answers common questions so you can confidently interpret the data and take appropriate actions That's the whole idea..

Counterintuitive, but true.

Understanding CPU Usage Metrics

Before diving into specific commands, it helps to know what the numbers actually represent. Even so, cPU usage is typically shown as a percentage of total processing capacity. Modern CPUs have multiple cores, and each core can be counted separately. That's why, a “100 %” reading on a single‑core system means the processor is fully utilized, while on a four‑core system, 100 % across all cores indicates that each core is working at full speed.

  • User time – time spent executing processes in user space.
  • System time – time spent in kernel mode handling interrupts and system calls.
  • Idle time – the proportion of time the CPU spends doing nothing.

When you run a command to find cpu usage in linux, the output will usually aggregate these metrics into an overall percentage that reflects the combined load on all cores.

Common Commands to Check CPU Usage

The top Command

The classic top utility is often the first tool that comes to mind when you need to see CPU usage instantly. Running top displays a live, updating view of system processes and a summary bar at the top that includes:

  • %CPU – the percentage of CPU used by all processes.
  • %MEM – memory consumption (useful for correlation).

Key features

  • Press Shift + P to sort processes by CPU usage.
  • Press 1 to toggle per‑core breakdown, showing each core’s individual load.
  • Use k to kill a misbehaving process after identifying it.

The htop Command

If you prefer a more user‑friendly interface, htop offers a color‑coded, interactive display. Install it via your package manager (e.Worth adding: g. , sudo apt install htop).

  • Scrollable process list with columns for CPU, memory, and command line.
  • F3 to search for a specific process.
  • F6 to change the sorting order.
  • F4 to filter processes by name or user.

Because htop updates in real time, it is especially handy when you need to spot spikes quickly That's the part that actually makes a difference..

The ps Command

While ps does not provide a live view, it can be combined with other utilities to produce a snapshot of CPU usage. For example:

ps -eo pid,user,pcpu,pmem,comm --sort=-pcpu | head -n 10

This command lists the top 10 processes sorted by CPU consumption (-pcpu). The -e flag enumerates all processes, and -o specifies the output columns.

The vmstat Command

vmstat reports virtual memory statistics, but its r (runnable) and b (blocked) columns give indirect insight into CPU load. A quick view:

vmstat 1

The first column shows CPU usage as a percentage of total capacity; values above 80 % indicate heavy load.

The sar Command

Part of the sysstat package, sar can record and replay CPU usage history. To view recent CPU utilization:

sar -u 1 3

This samples CPU usage every second (1) three times (3). The -u flag focuses on user and system CPU times.

The mpstat Command

mpstat is another sysstat tool that reports per‑CPU statistics. Example:

mpstat -P ALL 2 5

It prints CPU statistics for all cores (-P ALL) every 2 seconds for 5 iterations. This is useful for diagnosing imbalance across cores.

Reading from /proc

Linux exposes detailed CPU information in the /proc filesystem. The file /proc/stat contains a line that begins with cpu followed by several numbers representing time spent in different states. A simplified extraction looks like:

awk '/^cpu / {for (i=2; i<=10; i++) sum+=$i} END {print 100 - (sum/610)}' /proc/stat

This one‑liner calculates the idle percentage and subtracts it from 100 to obtain the active CPU usage. Though more complex, it offers granular control for scripting.

Practical Examples

Below are step‑by‑step examples that illustrate how to use each command in real‑world scenarios Small thing, real impact..

Example 1 – Quick Check with top

  1. Open a terminal.
  2. Run top.
  3. Observe the %CPU column in the summary line.
  4. If a single process is consuming an unusually high percentage, press Shift + P to sort and locate it.

Example 2 – Monitoring All Cores with htop

  1. Install htop (sudo apt install htop).
  2. Execute htop.
  3. Press 1 to split the display into per‑core columns.
  4. Watch the bars for each core; a core that is constantly near 100 % may indicate a thread‑pool overload.

Example 3 – Snapshot of Top CPU Consumers with ps

ps -eo pid,user,pcpu,pmem,comm --sort=-pcpu | head -n 5
  • Output: PID, user, CPU %, memory %, and command name for the five most CPU‑intensive processes.
  • Use this data to decide whether to restart, throttle, or investigate a process further.

Example

Out Now

Fresh Out

You Might Find Useful

Before You Go

Thank you for reading about Command To Find Cpu Usage In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home