Bash script to monitor top 10 RAM and CPU consuming process Linux

CPU Consuming Process
RAM and CPU Consuming Process

Monitor top 10 RAM & CPU consuming process


In this article, I will share with you a script that helps you to find out the top 10 RAM and CPU consuming process.

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

Conditions Use:

In this script, I am 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 the server process is a very important thing for every system admin.

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

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

Read: Steps To Install Postfix Server on Linux

Install the “bc” Package

Before using this script install ‘bc‘ on the server.

bc will help to calculate the average RAM & CPU utilization. Use below apt-get command to install bc package,

$ apt-get update
$ apt-get install bc

Bash Script |

vim /opt/scripts/


##In the 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'
MAIL_TO='[email protected]'

##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

Change Script File Permission

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

$ chmod +x

Schedule CronJob

Now setup Cron job which will be executed every 5 minutes. By using Cron you will be able to monitor the server every five minutes.

vim /etc/crod.d/script

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

Save and close the file.

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

You can modify the 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.


Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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