Bash script to monitor top 10 Memory consuming process

Monitor top 10 Memory consuming process


In this article, I will provide you script which helps you to find out top 10 memory consuming process.

I have written this script when one of our clients wants email for every top consuming process on the server.

Conditions Use:

In this script, I have collecting information from servers when it's on high utilization.

Below are the conditions used in the script to monitor the process.

  • Send Email When CPU utilization above 60%
  • Top 10 CPU Utilization Process
  • Top 10 Memory Utilization Process

As we all know monitoring server process is the very important thing for every system admin.

This script will help you to know which process eats CPU and Memory.
This is complete automated script which will send you an email notification when above conditions are true.

Make sure you have a mail server configured on your server. if not then follow below link which helps you to configure a mail server.


Read : Steps To Install Postfix Server on Linux


Install bc :

Before using this script install 'bc' on the server. bc will help to calculate the average of CPU utilization.

$ apt-get install bc

Bash Script

vim /opt/scripts/


## In below variable we are storing file names which help to store results.

## This variable store the Mail Body. Ise your email id in MAIL_TO Variable.
SUBJECT="Warning :: Web_Server1 CPU Utilization High"
MAIL_BODY='Hi,nnServer utilization is high.n'

## Below cat command will empty files after sending email alert and server utilization is normal.
cat /dev/null > $TOPCPU_TMP
cat /dev/null > $TOPPROC_TMP

## For loop and count will help to check no. CPU process running and stores top 10 processes in $TOPPROC_TMP variable.
for count in {1..150}; do
top -b -n1 -d1 | head -n17 | grep '%Cpu(s):' >> $TOPCPU_TMP
top -b -n1 -d1 | head -n17 | egrep -v "top|PID|USER|COMMAND" | tail -n10 >> $TOPPROC_TMP

## Echo command will send you complete report in Email.

## In Average_cpu we will store the average of CPU.
AVERAGE_CPU=$(echo "100 - `awk '{print $8}' $TOPCPU_TMP | awk '{ sum += $1 } END { if (NR > 0) print sum / NR }' | awk -F '.' '{print $1}'`" | bc)

## Echo will store current cpu utilization in $CPU_MONITOR_RESULT
echo -e "Current CPU utilization: ${AVERAGE_CPU}%n" >> $CPU_MONITOR_RESULT

## This code will save the Top 10 CPU consuming Process.
echo -e "Top 10 CPU consuming process:n" >> $CPU_MONITOR_RESULT
printf "%20s %10s %8sn" "Process" "Utilization" "PID" >> $CPU_MONITOR_RESULT
sort -r -n -k9 $TOPPROC_TMP | awk '!_[$1]++' | head -n10 | awk '{printf("%20s %10s %10s", $NF, $9"%", $1"n");}' >> $CPU_MONITOR_RESULT

## This code will save the Top 10 memory consuming Process.
echo -e "nTop 10 Memory consuming process:n" >> $CPU_MONITOR_RESULT
printf "%20s %10s %8sn" "Process" "Utilization" "PID" >> $CPU_MONITOR_RESULT
sort -r -n -k10 $TOPPROC_TMP | awk '!_[$1]++' | head -n10 | awk '{printf("%20s %10s %10s", $NF, $10"%", $1"n");}' >> $CPU_MONITOR_RESULT

## This condition will check the CPU utilization if it's above 60% then send the top 10 memory consuming process to result in an email.
if [ "${AVERAGE_CPU}" -ge 60 ]; then

Save above script in "/opt/scripts" directory with name "".

Permissions :

Once you save the script, provide executable permission to run the script.

$ chmod +x

Scheduled Cron:

Now setup Cron job which will be executing in every 5 minutes. By using cron you will be able to monitor server in every five minutes.

vim /etc/crod.d/script

*/5 * * * * root /bin/bash /opt/scripts/

Save and close the file.

This is well-tested script and provide you data without using more memory on the server.

You can modify above script according to your requirement. If you still face any issues to setup this script then comments down below in comment box. Stay connected & subscribe with for more such articles.