Bash script to monitor memory usage on Linux

There are many open source monitoring tools are currently available in market to monitor Linux systems performance which will send an email alert when the system reaches the specified threshold limit.

It monitors everything such as CPU utilization, Memory utilization, swap utilization, disk space utilization and much more.

If you only have few systems and want to monitor them then writing a small shell script can make your task very easy.

In this tutorial we have added two shell script to monitor Memory utilization on Linux system.

When the system reaches the given threshold then it will trigger a mail to given email id.

Method-1 : Linux shell script to monitor memory usage with email alert

If you want to get only the current Memory utilization percentage through mail when the system reaches the given threshold, use the following script.

This is very simple and straightforward one line script. I preferred to go with this method for most of the time.

This will trigger an email when more than 60% of your system memory is used.

# vi /opt/scripts/memory-alert.sh

#!/bin/bash
musage=$(free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' |  awk '{print $3}' | cut -d"." -f1)
if [ $musage -ge 60 ]; then
echo "Current Memory Usage: $musage%" | mail -s "Memory Usage on $(hostname) at $(date)" [email protected]
else
echo "Memory usage is in under threshold"
fi

Note:  Please change the email id and Memory utilization threshold value as per your requirement

Set an executable permission to the file “memory-alert.sh” :

# chmod +x /opt/script/memory-alert.sh

Run the following script to see if it works as expected:

# sh /opt/script/memory-alert.sh

Output: You will be getting an email alert similar to below.

Current Memory Usage: 65%

Method-2 : Linux bash script to monitor memory utilization with email alert

If you want to get more information about the Memory utilization in the mail alert.

Then use the following script, which includes top Memory utilization process details based on the top Command and ps Command.

This will immediately notify you of what is happening on your system and trigger an email when your system reaches more than 45% of memory.

Note: Please change the email id and Memory utilization threshold value as per your requirement.

# vi /opt/scripts/memory-alert-1.sh

#!/bin/bash
ramusage=$(free | awk '/Mem/{printf("RAM Usage: %.2f\n"), $3/$2*100}'| awk '{print $3}' | cut -d. -f1)

if [ $ramusage -ge 45 ]; then
SUBJECT="ATTENTION: Memory Utilization is High on $(hostname) at $(date)"
MESSAGE="/tmp/Mail.out"
TO="[email protected]"

  echo "Memory Current Usage is: $ramusage%" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Processes Using top command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(top -b -o +%MEM | head -n 20)" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "Top Memory Consuming Processes Using ps command" >> $MESSAGE
  echo "------------------------------------------------------------------" >> $MESSAGE
  echo "$(ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head)" >> $MESSAGE
  mail -s "$SUBJECT" "$TO" < $MESSAGE
  rm /tmp/Mail.out
  fi

Finally add a cronjob to automate this. It will run every 5 minutes.

# crontab -e
*/5 * * * * /bin/bash /opt/scripts/memory-alert-1.sh

Note: Since the script is scheduled to run once every 5 minutes, you will receive an email alert every 5 minutes.

Say for example If your system reaches the given limit after  8.25 minutes  then you will be getting an email alert on the second cycle i.e after 10 minutes ( 2nd 5 minute cycle)

Output: You will receive an email alert similar to the one below.

Conclusion

In this guide, we’ve added two shell scripts to monitor memory usage on the Linux system, which will trigger an email when it reaches a given threshold or above.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

9 Comments on “Bash script to monitor memory usage on Linux”

  1. How to handle errors if we face any? For eg: If the code doesn’t return the data (for any xyz reason) OR if any kind of uncertainties happen . Then what is the sccript for that?

  2. Forgot to add that if you want to use -ge condition instead of > you must change ramusage to int type, you can do so by just by adding “print int($3)”:
    ramusage=$(free | awk ‘/Mem/{printf(“RAM Usage: %.2f\n”), $3/$2*100}’| awk ‘{print int($3)}’)

  3. these scripts are bullshit they return an alert even if the threshold is not reached and they don’t allow for modification of the threshold percentage

    1. Are you referring to “Method-1”? If yes, this should be fixed. We are already working on this and will update the article with the solution as soon as possible.

Leave a Reply

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