When a Linux server suddenly becomes sluggish—web pages take seconds to load, SSH connections lag, and database queries timeout—the most common culprit is CPU exhaustion. A single rogue script stuck in an infinite loop, or a sudden spike in legitimate web traffic, can consume 100% of your processor’s capacity, leaving no resources for other essential tasks. As a system administrator, your first diagnostic step must always be identifying exactly which processes are eating your CPU cycles.
Understanding CPU Usage in Linux
Unlike a simple speedometer, Linux CPU usage is a complex metric. If you have a quad-core processor, your total available CPU capacity is technically 400%. A single application might consume 100% (maxing out one core) while leaving the other three cores perfectly idle, meaning your server is still healthy overall. Conversely, if a multi-threaded application consumes 390%, your entire system will grind to a halt. Diagnostic tools help you visualize this distribution.
Method 1: The ‘top’ Command (The Universal Standard)
The top command is installed on virtually every Linux distribution by default. It provides a live, continuously updating dashboard of system performance.
- Open your terminal or connect via SSH.
- Type
topand press Enter. - The screen will split into two sections. The top section shows system-wide averages, while the bottom section lists individual processes.
Reading the top output:
- Look at the third line, labeled %Cpu(s). The most important metric here is id (idle). If the
idnumber is low (e.g., 5.0 id), it means your CPU is 95% busy. - Now look at the process list below. By default,
topsorts the list by the %CPU column. The application at the very top of the list is the one consuming the most processing power. - Note the PID (Process ID) of the offending application. If the process is a frozen script you need to terminate, you will use this PID with the
killcommand. - To exit the dashboard and return to the normal terminal prompt, press the q key.
Method 2: The ‘htop’ Command (The Better Alternative)
While top is powerful, it is visually dense and somewhat difficult to read. The htop command is a modernized, interactive alternative that uses color-coding and graphical bar charts to make CPU analysis instantly intuitive.
- You likely need to install it first.
- Ubuntu/Debian:
sudo apt install htop - CentOS/RHEL:
sudo dnf install htop
- Ubuntu/Debian:
- Once installed, type
htopand press Enter.
Why htop is superior:
- At the very top of the screen,
htopdisplays a visual bar chart for every individual CPU core (numbered 1, 2, 3, 4, etc.). You can instantly see if the load is balanced across the entire processor or if a single core is maxed out. - The process list is color-coded.
- You can use your mouse (or arrow keys) to scroll vertically through the entire process list and horizontally to read long command strings.
- You can kill a process directly from the interface. Simply highlight the rogue process with the arrow keys and press the F9 key, then press Enter to send the kill signal.
- Press F10 (or q) to exit.
Method 3: Checking Historical Usage with ‘sar’
top and htop only show what is happening right now. If your server crashed at 3:00 AM and you want to know what the CPU usage was at that exact time, you need a historical logging tool like sysstat (which includes the sar command).
- Install it:
sudo apt install sysstat(You may need to enable data collection in/etc/default/sysstat). - To view CPU usage for the current day, broken down in 10-minute intervals, simply type:
sar -u
By utilizing top for quick checks, htop for interactive management, and sar for historical forensics, you can ensure your Linux server’s processing power is always optimized.