Managing High-CPU Usage Processes on Pop!_OS: A Practical Guide

As a Pop!_OS user, you might occasionally encounter situations where processes consume excessive CPU resources, slowing down your system and impacting performance. This guide walks you through an efficient, professional method to identify and manage high-CPU usage processes via the terminal. We will list the top resource-intensive processes, kill the ones exceeding a set threshold, and confirm the actions—all in a single script.

Why Monitor High-CPU Usage Processes?

High-CPU usage processes can cause:

  • System lag or unresponsiveness.
  • Increased power consumption (important for laptops).
  • Overheating of hardware components.

By identifying and managing these processes, you can ensure a smoother and more efficient experience on Pop!_OS.


Step 1: List the Top 10 CPU-Consuming Processes

The first step is to identify the most resource-intensive processes running on your system. Open a terminal and run the following command:

echo "Top 10 processes by CPU usage:"
ps -eo pid,%cpu,comm --sort=-%cpu | head -n 11

This command:

  • Displays the Process ID (PID), CPU usage (%CPU), and the command name (COMM).
  • Sorts processes by CPU usage in descending order.
  • Shows the top 10 processes (excluding the header).

Example output:

Top 10 processes by CPU usage:
  PID  %CPU  COMMAND
  1234  85.2 chrome
  5678  72.4 firefox
  9101  51.3 some_process

Step 2: Kill Processes Exceeding the CPU Threshold

To automatically terminate processes consuming more than 50% of CPU, use the following script:

echo "Killing processes with CPU usage over 50%...\n"

ps -eo pid,%cpu,comm --sort=-%cpu | awk 'NR>1 && $2 > 50 {print $1, $2, $3}' | while read pid cpu cmd; do
  echo "Killing process PID=$pid (Command=$cmd) with CPU=$cpu%"
  kill -9 "$pid"
done

echo -e "\nDone."

What This Script Does:

  1. Lists High-CPU Processes: The ps command retrieves the list of all processes sorted by CPU usage, while awk filters those consuming more than 50% CPU.
  2. Kills Processes Above Threshold: For each filtered process, the script:
    • Prints a message detailing the PID, command name, and CPU usage.
    • Sends a kill -9 signal to terminate the process.
  3. Confirms Completion: A final message indicates the script has finished running.

Step 3: Test and Monitor

Before killing processes, it is a good idea to test the listing command separately to ensure no critical system processes are inadvertently terminated:

ps -eo pid,%cpu,comm --sort=-%cpu | awk '$2 > 50'

This will display processes consuming more than 50% CPU without taking any action. Review the output to confirm which processes you want to terminate.


Notes and Best Practices:

  1. Use with Caution: Killing processes forcefully (kill -9) can lead to data loss or system instability if critical processes are terminated. Always review the list before proceeding.
  2. Modify the Threshold: You can adjust the CPU usage threshold in the script by changing the 50 in $2 > 50 to a higher or lower value as per your needs.
  3. Automate for Convenience: Save the script in a file, such as kill_high_cpu.sh, and make it executable: chmod +x kill_high_cpu.sh Then, run it as needed with: ./kill_high_cpu.sh

Conclusion

By following these steps, you can effectively manage CPU-intensive processes on Pop!_OS, ensuring optimal system performance and stability. This script offers a professional yet straightforward approach to identifying and handling resource-hungry processes, giving you greater control over your system’s operations.

For more guides and tips on managing Pop!_OS, stay tuned to our blog!