- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
A Shell Script to Send Email Alert When Memory Gets Low
As we use our computers for various tasks, we often run multiple applications and software that consume a significant amount of memory. When available memory gets low, it can cause our computer to slow down or even crash. To avoid this situation, we can use a shell script to send an email alert when memory gets low. In this article, we'll discuss how to create such a shell script and explain how it works.
Understanding Shell Scripts
Before we dive into creating shell script, let's first understand what shell scripts are. A shell script is a program that consists of a series of commands written in a shell language, which is interpreted by operating system. Shell scripts are commonly used to automate repetitive tasks and system administration tasks.
Creating a Shell Script to Monitor Memory Usage
To create a shell script to monitor memory usage, we need to use following commands −
free − This command displays amount of free and used memory in system.
awk − This command is a powerful text-processing tool that allows us to manipulate and analyze data.
mail − This command sends an email to a specified email address.
Here is shell script that we'll create −
#!/bin/bash # Get memory usage details memory=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}') # Define threshold memory usage threshold=80.00 # Check if memory usage is greater than threshold if (( $(echo "$memory > $threshold" | bc -l) )); then # Send an email alert echo "Memory usage is above threshold: $memory" | mail -s "Memory Usage Alert" your_email_address@example.com fi
Let's go through this script line by line to understand what it does.
#!/bin/bash − This line tells operating system to use Bash shell to interpret script.
memory=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}') − This line gets memory usage details using free command and calculates percentage of used memory using awk command.
threshold=80.00 − This line defines threshold memory usage. In this example, we've set threshold to 80%.
if (( $(echo "$memory > $threshold" | bc -l) )); then − This line checks if memory usage is greater than threshold using if statement.
echo "Memory usage is above threshold − $memory" | mail -s "Memory Usage Alert" your_email_address@example.com: If memory usage is above threshold, this line sends an email alert to specified email address using mail command.
Using Shell Script
To use shell script, we need to follow these steps
Open a text editor and copy script code into it.
Replace "your_email_address@example.com" with your actual email address.
Save file with a .sh extension (e.g., mem_alert.sh).
Open terminal and navigate to directory where you saved file.
Make file executable by running following command: chmod +x mem_alert.sh.
Run shell script using following command: ./mem_alert.sh.
The shell script will now monitor memory usage and send an email alert if usage goes above threshold.
Customizing Shell Script
You can customize shell script to suit your needs by modifying following variables −
threshold − You can change threshold memory usage by modifying value of threshold variable. For example, if you want to set threshold to 90%, you can change line "threshold=80.00" to "threshold=90.00".
email_address − You can change email address to which alert is sent by modifying value of email_address variable. For example, if you want to send alert to "my_email@example.com", you can change line "your_email_address@example.com" to "my_email@example.com".
You can also modify message that is sent in email alert by modifying echo statement. For example, you can add more information to message or customize subject of email alert.
Benefits of Using a Shell Script to Monitor Memory Usage
Using a shell script to monitor memory usage has several benefits −
Automation
shell script automates process of monitoring memory usage and sending email alerts, which saves time and effort.
Proactive Monitoring
email alert is sent when memory usage goes above threshold, which allows you to take proactive measures to avoid any issues related to low memory.
Customization
shell script can be customized to suit your specific needs, such as changing threshold memory usage or email address to which alert is sent.
Conclusion
In this article, we discussed how to create a shell script to monitor memory usage and send email alerts when memory usage goes above a threshold. We explained various commands used in shell script and provided step-by-step instructions on how to use and customize script. By using a shell script to monitor memory usage, you can automate process of monitoring and take proactive measures to avoid any issues related to low memory.