How to Monitor Linux Memory Usage Using Bash Script

How to Monitor Linux Memory Usage Using Bash Script

Preface

There are many open source monitoring tools available in the market that can be used to monitor the performance of Linux systems. It can send email alerts when the system reaches specified threshold limits. It can monitor everything from CPU utilization, memory utilization, swap utilization, disk space utilization, and more.

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

In this tutorial, we added two shell scripts to monitor memory utilization on a Linux system. When the system reaches a given threshold, it will send an email to a specific email address.

Method-1: Monitor Memory Utilization and Send Email Using Linux Bash Script

If you only want to get the current memory utilization percentage via email when the system reaches a given threshold, use the following script.

This is a very simple and straightforward one-line script. In most cases, I prefer this approach.

When your system reaches 80% memory utilization, it will trigger an email.

*/5 * * * * /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' | awk '{print $3}' | awk '{ if($1 > 80) print $0;}' | mail -s "High Memory Alert" [email protected]

NOTE: You need to change your email address instead of using our email address. Additionally, you can change the memory utilization threshold as per your requirement.

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

High Memory Alert: 80.40%

We have added many useful shell scripts in the past. If you would like to view these, please navigate to the links below.

* How to automate daily activities using shell scripts?

Method-2 : Monitor Memory Utilization and Send Email Using Linux Bash Script

If you want to get more information about memory utilization in mail alerts. Use the following script which includes top memory utilization and process details based on top and ps commands.

This will immediately give you an idea of ​​how your system is performing.

When your system reaches "80%" of memory utilization, it will trigger an email.

NOTE: You need to change your email address instead of using our email address. Additionally, you can change the memory utilization threshold as per your requirement.

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

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

if [ "$ramusage" > 20 ]; 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 Process Using top command" >> $MESSAGE
 echo "------------------------------------------------------------------" >> $MESSAGE
 echo "$(top -b -o +%MEM | head -n 20)" >> $MESSAGE
 echo "" >> $MESSAGE
 echo "------------------------------------------------------------------" >> $MESSAGE
 echo "Top Memory Consuming Process Using ps command" >> $MESSAGE
 echo "------------------------------------------------------------------" >> $MESSAGE
 echo "$(ps -eo pid,ppid,%mem,%Memory,cmd --sort=-%mem | head)" >> $MESSAGE
 mail -s "$SUBJECT" "$TO" < $MESSAGE
 rm /tmp/Mail.out
fi

Finally add a cron job to automate this operation. It will run every 5 minutes.

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

NOTE: Because the script is scheduled to run every 5 minutes, you will receive an email reminder in a maximum of 5 minutes (but not 5 minutes, depending on the time).

For example, if your system reaches a given limit of 8.25, then you will receive an email alert within 5 minutes. Hope that's clear now.

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

Memory Current Usage is: 80.71%

+----------------------------------------------------------------------------------+
Top Memory Consuming Process Using top command
+----------------------------------------------------------------------------------+
top - 12:00:58 up 5 days, 9:03, 1 user, load average: 1.82, 2.60, 2.83
Tasks: 314 total, 1 running, 313 sleeping, 0 stopped, 0 zombie
%Cpu0 : 8.3 us, 12.5 sy, 0.0 ni, 75.0 id, 0.0 wa, 0.0 hi, 4.2 si, 0.0 st
%Cpu1 : 13.6 us, 4.5 sy, 0.0 ni, 81.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 21.7 us, 21.7 sy, 0.0 ni, 56.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 13.6 us, 9.1 sy, 0.0 ni, 77.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu4 : 17.4 us, 8.7 sy, 0.0 ni, 73.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 : 20.8 us, 4.2 sy, 0.0 ni, 70.8 id, 0.0 wa, 0.0 hi, 4.2 si, 0.0 st
%Cpu6 : 9.1 us, 0.0 sy, 0.0 ni, 90.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 : 17.4 us, 4.3 sy, 0.0 ni, 78.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 16248588 total, 5015964 free, 6453404 used, 4779220 buff/cache
KiB Swap: 17873388 total, 16928620 free, 944768 used. 6423008 avail Mem

 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
17163 daygeek 20 2033204 487736 282888 S 10.0 3.0 8:26.07 /usr/lib/firefox/firefox -contentproc -childID 15 -isForBrowser -prefsLen 9408 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /u+
 1121 daygeek 20 4191388 419180 100552 S 5.0 2.6 126:02.84 /usr/bin/gnome-shell
 1902 daygeek 20 1701644 327216 82536 S 20.0 2.0 153:27.92 /opt/google/chrome/chrome
 2969 daygeek 20 1051116 324656 92388 S 15.0 2.0 149:38.09 /opt/google/chrome/chrome --type=renderer --field-trial-handle=10346122902703263820,11905758137655502112,131072 --service-pipe-token=1339861+
 1068 daygeek 20 1104856 309552 278072 S 5.0 1.9 143:47.42 /usr/lib/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
27246 daygeek 20 907344 265600 108276 S 30.0 1.6 10:42.80 /opt/google/chrome/chrome --type=renderer --field-trial-handle=10346122902703263820,11905758137655502112,131072 --service-pipe-token=8587368+

+----------------------------------------------------------------------------------+
Top Memory Consuming Process Using ps command
+----------------------------------------------------------------------------------+
 PID PPID %MEM %CPU CMD
 8223 1 6.4 6.8 /usr/lib/firefox/firefox --new-window
13948 1121 6.3 1.2 /usr/bin/../lib/notepadqq/notepadqq-bin
 8671 8223 4.4 7.5 /usr/lib/firefox/firefox -contentproc -childID 5 -isForBrowser -prefsLen 6999 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
17163 8223 3.0 0.6 /usr/lib/firefox/firefox -contentproc -childID 15 -isForBrowser -prefsLen 9408 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 1121 1078 2.5 1.6 /usr/bin/gnome-shell
17937 8223 2.5 0.8 /usr/lib/firefox/firefox -contentproc -childID 16 -isForBrowser -prefsLen 9410 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 8499 8223 2.2 0.6 /usr/lib/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 6635 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 8306 8223 2.2 0.8 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab
 9198 8223 2.1 0.6 /usr/lib/firefox/firefox -contentproc -childID 7 -isForBrowser -prefsLen 8604 -prefMapSize 184979 -parentBuildID 20190521202118 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 8223 true tab

via: https://www.2daygeek.com/linux-bash-script-to-monitor-memory-utilization-usage-and-send-email/

Author: Magesh Maruthamuthu Topic: lujun9972 Translator: wxy Proofreader: wxy

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Launch a Git-Bash window in a specific directory using a batch script
  • Detailed explanation of script debugging mechanism in bash
  • Handling variables with spaces in shell script (bash script)
  • Bash script enables you to view Linux system information every time you log in to the shell
  • Detailed explanation of how to pass password to ssh/scp command in bash script
  • Example of how to identify the user using a linux Bash script
  • Detailed explanation of the differences between source, sh, bash, and ./ in shell script execution
  • Linux bash script monitoring start and stop weblogic service script writing
  • How to write secure, fully functional Bash scripts

<<:  Detailed explanation of how to view the number of MySQL server threads

>>:  Vue implements 3 ways to switch tabs and switch to maintain data status

Recommend

Nginx configuration 80 port access 8080 and project name address method analysis

Tomcat accesses the project, usually ip + port + ...

JavaScript ES6 Module Detailed Explanation

Table of contents 0. What is Module 1.Module load...

MySQL 8.0.11 installation tutorial with pictures and text

There are many tutorials on the Internet, and the...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...

Using react-virtualized to implement a long list of images with dynamic height

Table of contents Problems encountered during dev...

A brief analysis of the basic implementation of Vue detection data changes

Table of contents 1. Object change detection 2. Q...

Detailed description of common events and methods of html text

Event Description onactivate: Fired when the objec...

Summary of commonly used performance test scripts for VPS servers

Here is a common one-click performance test scrip...

css3 animation ball rolling js control animation pause

CSS3 can create animations, which can replace man...