Check CPU Usage in Linux
Check CPU Usage in Linux

Check CPU Usage in Linux

Monitoring CPU usage in Linux is crucial for system optimization, troubleshooting performance issues, and ensuring efficient resource utilization. Whether you're a system administrator, developer, or a regular user, understanding how to check CPU usage in Linux can help you maintain system stability and performance.

High CPU usage can slow down applications, cause overheating, and lead to unexpected crashes. By leveraging built-in commands and third-party tools, you can efficiently monitor CPU performance and take necessary actions to optimize your Linux system.

How to Check CPU Usage in Linux (Overview)

There are multiple ways to check CPU usage in Linux, including command-line utilities and graphical tools. Here’s an overview of the most effective methods:

  • Command-Line Tools: top, htop, mpstat, sar, iostat
  • Graphical Tools: GNOME System Monitor, KSysGuard, Netdata

Below, we explore each method in detail with usage examples.

Also Read:- install chrome for linux

Using the top Command

The top command is one of the most commonly used tools to check CPU usage in real time.

How to Use top Command:

Open a terminal and run:

 top        

This command displays an updated list of processes, including CPU and memory usage.

Understanding top Output:

  • %Cpu(s): Displays CPU usage breakdown into user, system, and idle processes.
  • PID: Process ID of running tasks.
  • %CPU: CPU percentage used by each process.
  • COMMAND: Name of the executing process.

To exit top, press q.

Also Read:- how to install splunk on linux

Using the htop Command (More User-Friendly Alternative)

htop is an enhanced, interactive version of top with an easier-to-read interface.

Installing htop:

sudo apt install htop   # Debian/Ubuntu
sudo yum install htop   # RHEL/CentOS
sudo pacman -S htop     # Arch Linux        

Running htop

htop        

htop provides a visually appealing, color-coded representation of CPU usage with an intuitive navigation system.

Checking CPU Usage with mpstat (Advanced Users)

The mpstat command (part of the sysstat package) provides CPU usage statistics for each processor core.

Installing mpstat

sudo apt install sysstat  # Debian/Ubuntu
sudo yum install sysstat  # RHEL/CentOS        

Using mpstat to Monitor CPU Usage:

mpstat 5 10        

This command samples CPU usage every 5 seconds for 10 iterations.

Using sar for Historical CPU Usage

The sar command helps track CPU performance trends over time.

Installing sar

sudo apt install sysstat  # Debian/Ubuntu        

Checking CPU Usage with sar

sar -u 5 10        

This command prints CPU utilization every 5 seconds for 10 cycles.

Monitoring CPU Usage with iostat

The iostat command provides reports on CPU and disk activity.

Installing iostat

sudo apt install sysstat  # Debian/Ubuntu        

Using iostat

iostat -c 5 10        

This displays CPU usage statistics every 5 seconds for 10 iterations.

Also Read:- how to turn on linux on chromebook

Graphical Tools for Checking CPU Usage in Linux

For users who prefer GUI-based monitoring, Linux provides various tools:

  • GNOME System Monitor: Pre-installed in GNOME-based distros.
  • KSysGuard: KDE’s system monitoring tool.
  • Netdata: A web-based real-time monitoring tool.

These tools offer a more user-friendly approach to CPU monitoring, displaying graphical statistics and trends over time.

How to Reduce High CPU Usage in Linux

If your system experiences high CPU usage, consider the following:

Identify and Kill High CPU Processes

Run:

top        

Locate the high CPU-consuming process and terminate it:

kill -9 <PID>        

Adjust Process Priorities

Lower the priority of a process using nice:

nice -n 10 <command>        

Or change priority of an existing process:

renice -n 10 -p <PID>        

Disable Unnecessary Services

List running services:

systemctl list-units --type=service        

Disable unwanted services:

sudo systemctl disable <service-name>        

Optimize System Performance

  • Reduce background processes.
  • Keep software and kernel updated.
  • Use lightweight alternatives for resource-intensive applications.

Frequently Asked Questions (FAQs)

How do I check real-time CPU usage in Linux?

You can check real-time CPU usage in Linux using the top or htop commands. top provides a basic real-time view, while htop offers a more user-friendly interface.

How can I monitor CPU usage per core in Linux?

The mpstat -P ALL command displays CPU usage per core. You can also use htop, which visually separates CPU usage for each core.

How do I log CPU usage over time?

Use the sar command to log CPU usage trends over time. For example:

sar -u 5 100 > cpu_log.txt        

This logs CPU usage every 5 seconds for 100 iterations.

What should I do if my CPU usage is too high?

  • Identify high CPU-consuming processes using top or htop.
  • Kill unnecessary processes using kill -9 <PID>.
  • Adjust process priorities using renice.
  • Disable unwanted background services with systemctl disable.

What is a normal CPU usage percentage in Linux?

Normal CPU usage varies depending on workload. Idle systems should be below 10%, while active workloads may reach 50-70%. Sustained usage over 90% may indicate performance issues.

Conclusion

Monitoring CPU usage in Linux is essential for maintaining optimal system performance. Whether using command-line tools like top, htop, mpstat, or GUI-based solutions like GNOME System Monitor, each method offers unique advantages.

By proactively checking CPU usage and optimizing performance, you can ensure your Linux system runs efficiently. Try out these commands and tools to monitor and manage your system resources effectively!


要查看或添加评论,请登录

Vivek Yadav的更多文章

  • Man Command in Linux

    Man Command in Linux

    Linux is a powerful operating system widely used for its flexibility, security, and efficiency. One of its most…

  • Most Popular Linux Distributions

    Most Popular Linux Distributions

    Linux has become one of the most widely used operating systems in the world, powering everything from personal…

  • Install OpenVAS on Ubuntu

    Install OpenVAS on Ubuntu

    OpenVAS (Open Vulnerability Assessment System) is a powerful open-source tool for vulnerability scanning and security…

  • How to Install Linux on Windows

    How to Install Linux on Windows

    Linux is a powerful, open-source operating system known for its flexibility, security, and performance. Many users want…

  • Install Chrome for Linux

    Install Chrome for Linux

    Google Chrome is one of the most popular web browsers globally, known for its speed, security, and extensive extension…

  • How to Install Splunk on Linux

    How to Install Splunk on Linux

    Splunk is a powerful platform for log management, monitoring, and security analytics. It helps businesses and IT…

  • How to Turn on Linux on Chromebook

    How to Turn on Linux on Chromebook

    Chromebooks are lightweight, fast, and secure devices primarily designed to run Chrome OS. However, if you need a more…

  • Unable to Locate Package Ubuntu

    Unable to Locate Package Ubuntu

    When using Ubuntu, you may encounter an error message that says "Unable to locate package Ubuntu." This error often…

  • Ubuntu vs CentOS

    Ubuntu vs CentOS

    When choosing a Linux distribution for your server or personal use, two of the most popular options are Ubuntu and…

  • RedHat vs Ubuntu

    RedHat vs Ubuntu

    Linux distributions play a vital role in powering servers, cloud computing, and enterprise environments. Among the most…

    1 条评论