Linux is a popular operating system that allows users to run multiple processes at the same time on their systems. As we run more and more applications and utilities on our system, more and more processes are started on our system and over time they can slow down our system. So it is important to keep an eye on CPU consumption of processes. In this article, we will learn how to find top CPU consuming processes in Linux. It is important for system administrators to monitor top CPU consuming processes on a regular basis and kill any process that is unnecessarily taking up too many system resources.
Sometimes your application or process may become slow or unresponsive simply because there are too many processes running on your system, or because some of the processes are taking up too many system resources. In such cases, it is helpful to find out which processes are causing the problem.
How to Find Top CPU Consuming Processes in Linux
There are several utilities to help you monitor CPU processes. Some of them are third party tools. But we will use two simple processes that are already present in most Linux systems – top and ps.
1. Using Top Command
Running Top command will directly list all processes on your system sorted in descending order of their resource consumption.
# top
Please note, the output of top process updates itself regularly based on latest information, until you stop it using Ctrl+C keys.
If you want to view only the top memory consuming processes, then use the -o option as shown below.
# top -o %MEM
Similarly, there is also htop utility that pretty much gives the same information except that it allows you to scroll the output horizontally as well as vertically, whereas top command allows you to scroll only vertically.
2. Using ps Command
You can also use ps command to find top consuming process. Here is a simple command to help you to find top consuming processes sorted by cpu usage and memory consumption.
# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Here is a sample output.
PID PPID CMD %MEM %CPU 2391 2113 /usr/lib/chrome/chrome 5.3 47.5 2249 2520 /usr/lib/virtualbox/Virtual 4.4 4.2 2188 1 /home/feghnepa/.dropbox-dis 2.4 0.3 1989 1543 /usr/lib/python 2.0 0.2 2013 1801 /usr/bin/node 0.9 2.5 2154 2252 python /usr/bin/linuxmint/m 0.8 0.0 2235 1801 nautilus -n 0.5 0.1 1545 1595 /usr/bin/celery 0.3 2.5
In the above command, ps commands lists all processes running on your system. Option -o lets you specify the output that is the specific fields you want to view. -sort option allows you to specify sort order and sort field. We sort by memory field by mentioning %mem and add a – sign to indicate descending order. Default sort order is ascending, if you do not mention the – sign. Here is an example.
# ps aux --sort -%mem
If you want to view only the top 10 processes that consume most resources then pass the output of ps command to head command as shown below.
# ps aux --sort -%mem | head -10
In this article, we have learnt how to find top CPU consuming processes in Linux. You can use these steps in almost every Linux system.
Also read:
How to Install Fail2ban in Ubuntu
How to Open Port in Linux
How to Create Remote Git Repository
How to Enable Keep Alive in NGINX
How to Use Git Shallow Clone
Related posts:
Sreeram Sreenivasan loves coding & writing tech articles. He has helped many Fortune 500 companies in the areas of BI & software development. He has more than 10 years of experience in web development, Python, Linux, SQL and database programming.