Command to get top ten processes consuming most CPU

When you are running a web server it is really important to keep the track of process which are consuming most resources. We will see the command to get top ten processes consuming most CPU

Command to get top ten processes consuming most CPU
Command to get top ten processes consuming most CPU

Command to get top ten processes consuming most CPU

Such tracking is important when there is a DOS attack and you need to block the ip from where the attack is happening. In such cases having a script which will give the top ip which are hitting your server.

Have a look at the below command.

ps aux| awk '{print $3, $2}' | head -n 15

What this command will do:

This will print the list of ip which are most frequently hitting your server in sorted order. Lets break down the command and see what is happening here.

ps aux

This command will get all the process with details and then we piped the output as input to the next command. Next command  is

awk '{print $3, $2}' 

This will take print the CPU usage and process id from the results because CPU usage is in third place and process id  is present in second place in result.

After this we have to get the top 15 lets say for this we will use head command as below

head -n 15

Thus we will get the list of process which are consuming most CPU along with their process id.  The output will be something like below

 
%CPU PID

98.1 166

5.8 63

1.7 706

1.3 709

0.8 449

0.4 718

0.3 713

0.3 157

0.2 484

0.1 445

0.1 707

0.1 717

0.1 524

0.1 687
First param is CPU usage and second param is process id

So this is how you get the processes and for killing the process use below command

pkill process_id

You can also use tools like htop for better process management.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

1 COMMENT
  • How to get number of threads per process? - Learn Steps
    Reply

    […] Command to get top ten processes consuming most CPU […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.