Linux is quite popular for its command-line utilities, which not only make any task at hand easier but also saves a lot of time, which is otherwise wasted in graphical UI based utilities.
This is one of the reasons why Linux is a preferred operating system for servers and administrative machines. Combine the knowledge of Linux commands and shell scripting and you have a proper toolkit of system administration at your disposal.
Today we will see how to see the top 10 heaviest memory and CPU resource-consuming processes in Linux using a command-line tool called ps command, which is used to display information about running processes in Linux.
List All Running Processes in Linux
The ps command has a lot of options and arguments available to display output in different formats. However, it can be simply run with the argument ‘aux’ to get a general overview of running processes.
$ ps aux
As we can see above, CPU and memory usage are shown in the columns %CPU and %MEM respectively along with other information about the running processes.
Now, we will use the argument '--sort'
to sort the output by memory and CPU usage.
The syntax for using the sort argument is:
$ ps aux --sort <column_name>
Similarly, to reverse sort the output, the following can be used:
$ ps aux --sort -<column_name>
Any column from the output can be specified in <column_name>.
List Top 10 Linux Processes by Memory and CPU Usage
Since we want the top 10 processes by memory and CPU usage, we will sort the output by reverse, and put these columns as the column name argument.
Find Top Running Processes by Memory Usage
$ ps aux --sort -%mem
Find Top Running Processes by CPU Usage
$ ps aux --sort -%cpu
Finally, to limit the number of processes shown to 10, we pipe the output to the head command.
$ ps aux --sort -%mem | head -10 $ ps aux --sort -%cpu | head -10
Note that due to some command names being longer and using a new line in the output, ‘head -10’ will consider this new line. You can simply increase the argument to ‘head’ in such cases, for example, to 12 or 14 whichever will bring the number of output processes to 10.
Conclusion
In this article, we saw how to use the ps command to see top resource-consuming processes in Linux. The ps command is a complex command and mastering it comes a long way in properly administering the system and even for using the output in an automation script.
Do refer to the manual page of ps command by running the command:
$ man ps
If you have any questions or feedback, let us know in the comments below.