AllTechWiki



A Place for All Technical Stuff Linux Cloud etc

How to identify which process is consuming high CPU Memory in Linux Redhat Ubuntu

We can identify the process which is consuming high CPU or Memory in Linux Redhat/ Ubuntu / SUSE etc by using simple top or htop command in live.

But what if my system is behaving high consumption in random time ? how we can identify in such scenario.

Yes, in most of time VM will consume High CPU or Memory at random time due to large number of operation happing at that particular time. 

By using below script we can capture the TOP process in text file and we can analyze it later , so that we can understand what went wrong during at that time.

Please follow below steps and implement in any Linux distribution , so that it will be helpful for all Linux Admins.


Run below commands under root user

root@Ubuntu18-JumpBox:~# mkdir -p /var/log/topcpu-identify

root@Ubuntu18-JumpBox:~# cd /var/log/topcpu-identify

root@Ubuntu18-JumpBox:~# touch topcpu-identify-output.txt

root@Ubuntu18-JumpBox:~# touch topcpu-identify-script.sh

root@Ubuntu18-JumpBox:~# chmod 755 topcpu-identify-output.txt topcpu-identify-script.sh 

root@Ubuntu18-JumpBox:~#  vi topcpu-identify-script.sh

#!/bin/bash

export COLUMNS=1000;

OFILE=/var/log/topcpu-identify/topcpu-identify-output.txt;

echo "================================ DATA START ===============================" >> $OFILE;

date >> $OFILE;

top -o +%CPU -bc -n 1| head -n 20 >> $OFILE;

#SCRIPT END


RUN BELOW CRONJOB UNDER ROOT USER

root@Ubuntu18-JumpBox:~#  crontab -e 

##script to run every one minute to identify the proces which consuming high & you can change time as per your requirement ##

* * * * *  bash /var/log/topcpu-identify/topcpu-identify-script.sh

The above cronjob runs for every one min and you can change it for 2 min, 5 min , 10 min etc based on your choice.

All the Top process will be capturing and saving in the file /var/log/topcpu-identify/ topcpu-identify-output.txt and you can analyze it for later and identify the process.

The Example is below

Script File



root@Ubuntu18:/var/log/topcpu-identify# cat topcpu-identify-output.txt


Please implement in Linux VM's if face performance issues in randomly.



Post a Comment

Previous Post Next Post

Basic Useful Links