SurferCloud Blog SurferCloud Blog
  • HOME
  • NEWS
    • Latest Events
    • Product Updates
    • Service announcement
  • TUTORIAL
  • COMPARISONS
  • INDUSTRY INFORMATION
  • Telegram Group
SurferCloud Blog SurferCloud Blog
SurferCloud Blog SurferCloud Blog
  • HOME
  • NEWS
    • Latest Events
    • Product Updates
    • Service announcement
  • TUTORIAL
  • COMPARISONS
  • INDUSTRY INFORMATION
  • Telegram Group
  • banner shape
  • banner shape
  • banner shape
  • banner shape
  • plus icon
  • plus icon

How to Check CPU Usage in Linux: A Comprehensive Guide

December 18, 2024
5 minutes
TUTORIAL
503 Views

Monitoring CPU usage is crucial for maintaining system performance, diagnosing bottlenecks, and optimizing resource allocation. On a Linux server, high CPU usage can indicate overloaded processes, misbehaving applications, or the need for hardware upgrades.

This guide will walk you through several practical methods to check CPU usage in Linux. We’ll also highlight how SurferCloud’s high-performance Linux VPS can help you manage and optimize system resources with ease.


Why Monitor CPU Usage in Linux?

Monitoring CPU usage allows you to:

  • Detect system slowdowns: Identify resource-hogging applications.
  • Enhance performance: Improve system efficiency by terminating unnecessary processes.
  • Plan resource allocation: Ensure your server has sufficient resources to handle peak loads.
  • Optimize server costs: Avoid unnecessary server upgrades by reallocating CPU resources.

If you're running applications like web servers, databases, or containerized workloads, tracking CPU usage is essential for stable system performance.


How to Check CPU Usage in Linux

There are several commands and tools in Linux to check CPU usage. Each method provides a unique perspective on system performance, from real-time monitoring to historical data analysis.


1. top Command

The top command is one of the most well-known tools to view live CPU usage, memory usage, and system processes. It provides real-time updates every few seconds.

Command:

top

Key Fields in top:

  • %CPU: The percentage of CPU used by each process.
  • PID: Process ID of each running process.
  • COMMAND: The name of the process or command running.

To sort processes by CPU usage, press Shift + P while in the top interface.

Usage Example:

  1. Run top.
  2. Look at the %CPU column to see which processes are using the most CPU.

2. htop Command

The htop command is a user-friendly alternative to top, offering a colorful, interactive display of system resource usage. It provides a more visual representation of CPU cores and load.

Command to Install htop:

sudo apt-get install htop  # For Debian/Ubuntu
sudo yum install htop      # For RHEL/CentOS

Command to Run htop:

htop

Benefits of htop:

  • Interactive Interface: Use arrow keys to scroll through processes.
  • Color-Coded Visualization: View CPU usage per core in real-time.
  • Easier Process Management: Kill processes directly from the htop interface.

3. vmstat Command

The vmstat (virtual memory statistics) command provides information on system resource usage, including CPU usage, memory, and I/O activities.

Command:

vmstat 1 5

Explanation of Command:

  • 1: Refreshes the data every second.
  • 5: Displays 5 updates.

Key Fields in vmstat:

  • us: Percentage of CPU time used by user processes.
  • sy: Percentage of CPU time used by system processes.
  • id: Percentage of time the CPU is idle.

If the id value is low, it means the CPU is being heavily used.


4. mpstat Command

The mpstat command is part of the sysstat package and provides a detailed view of CPU usage, especially on multi-core systems.

Command to Install sysstat (if not installed):

sudo apt-get install sysstat  # For Debian/Ubuntu
sudo yum install sysstat      # For RHEL/CentOS

Command to Run mpstat:

mpstat 1 5

Key Fields in mpstat:

  • %usr: User space CPU usage.
  • %sys: System space CPU usage.
  • %idle: Percentage of CPU idle time.

Use mpstat to view the CPU usage of each core separately, especially useful on multi-core servers.


5. sar Command

The sar (System Activity Report) command displays historical CPU usage data. Unlike top and htop, it shows resource usage over time.

Command to Install sysstat (if not installed):

sudo apt-get install sysstat  # For Debian/Ubuntu
sudo yum install sysstat      # For RHEL/CentOS

Command to Run sar:

sar -u 5 10

Explanation of Command:

  • -u: Show CPU usage.
  • 5: Report data every 5 seconds.
  • 10: Collect 10 samples.

Use sar to generate system usage reports, useful for performance audits or trend analysis.


6. uptime Command

The uptime command provides a quick summary of system load and how long the system has been running.

Command:

uptime

Output Example:

 08:52:21 up 10 days,  3:21,  2 users,  load average: 0.15, 0.07, 0.03

Explanation of Load Average:

  • 0.15 (1-minute average), 0.07 (5-minute average), 0.03 (15-minute average).
  • If these numbers are consistently higher than the number of CPU cores, it indicates that the system is overloaded.

7. /proc/stat File

For advanced users, you can analyze CPU usage data directly from the /proc/stat file. This file contains information on system statistics, including CPU usage.

Command to View /proc/stat:

cat /proc/stat | grep '^cpu '

Fields in /proc/stat:

  • user: Time spent in user mode.
  • nice: Time spent running processes with reduced priority.
  • system: Time spent in kernel mode.
  • idle: Time spent in an idle state.

By analyzing these fields, you can calculate CPU usage using the following formula:

CPU Usage = 100 * (user + nice + system) / (user + nice + system + idle)

How to Reduce High CPU Usage in Linux

If you find that CPU usage is consistently high, here are some tips to resolve it:

  1. Kill Resource-Intensive Processes: Use kill or htop to end high-CPU processes.
  2. Optimize Applications: Check if apps like databases or web servers need optimization.
  3. Use Cron Jobs During Off-Peak Hours: Schedule heavy tasks during periods of low server usage.
  4. Check for Malware/Mining Scripts: Use clamav to check for malware or mining scripts.
  5. Upgrade Your Server: If high CPU usage is persistent, consider upgrading to a more powerful VPS.

Why Choose SurferCloud VPS for Resource Management?

If you're managing a production server, CPU usage is a critical factor for performance. SurferCloud offers scalable, high-performance Linux VPS servers, enabling you to manage resources efficiently.

Benefits of SurferCloud Linux VPS:

  • Global Data Centers: Enjoy low-latency access across Asia, Europe, and the US.
  • On-Demand Scaling: Add CPU and memory when your application needs more power.
  • Enhanced Security: Protect your server from DDoS attacks.
  • 24/7 Support: Get assistance when you need it most.

With SurferCloud, you can enjoy optimal resource management, ensuring your applications stay fast and responsive.


Conclusion

Checking CPU usage in Linux is essential for system performance and stability. Whether you use top, htop, vmstat, or sar, each tool provides valuable insights into how your CPU is being utilized.

For businesses and developers managing Linux VPS servers, SurferCloud offers reliable and scalable cloud solutions. Take control of your server’s performance, monitor resource usage, and scale up with SurferCloud’s powerful cloud services.

Tags : check linux cpu usage htop vs top linux cpu monitoring linux performance analysis linux server monitoring Linux VPS hosting monitor cpu usage server resource optimization SurferCloud VPS top command linux

Related Post

5 minutes TUTORIAL

How to Rip Audio from YouTube to WAV Format o

Ripping audio from YouTube videos is a common task for ...

5 minutes INDUSTRY INFORMATION

How to Supercharge Ubuntu VPS Performance: Ex

An optimized Ubuntu VPS can make a dramatic difference ...

3 minutes TUTORIAL

How to Set Up Remote Access on a Fedora VPS:

Remote access is an essential feature for managing a Fe...

Affordable CDN

ucdn

2025 Special Offers:

annual vps

Light Server promotion:

ulhost-promo

Cloud Server promotion:

cloud server

Copyright © 2024 SurferCloud All Rights Reserved.  Sitemap.