A powerful aspect of Unix/Linux shell programs such as bash, is their amazing support for common programming constructs that enable you to make decisions, execute commands repeatedly, create new functions, and so much more. You can write commands in a file known as a shell script and execute them collectively.
This offers you a reliable and effective means of system administration. You can write scripts to automate tasks, for instance daily back ups, system updates etc; create new custom commands/utilities/tools and beyond. You can write scripts to help you keep up with what’s unfolding on a server.
One of the critical components of a server is memory (RAM), it greatly impacts on overall performance of a system.
In this article, we will share a small but useful shell script to send an alert email to one or more system administrator(s), if server memory is running low.
This is script is particularly useful for keeping an eye on Linux VPS (Virtual Private Servers) with small amount of memory, say of about 1GB (approximately 990MB).
Testing Environment Setup
- A CentOS/RHEL 7 production server with mailx utility installed with working postfix mail server.
This is how the alertmemory.sh script works: first it checks the free memory size, then determines if amount of free memory is less or equal to a specified size (100 MB for the purpose of this guide), used as a bench mark for the least acceptable free memory size.
If this condition is true, it will generate a list of the top 10 processes consuming server RAM and sends an alert email to specified email addresses.
Note: You will have to make a few changes to script (especially the mail sender utility, use the appropriate flags) to meet your Linux distributions requirements.
#!/bin/bash ####################################################################################### #Script Name :alertmemory.sh #Description :send alert mail when server memory is running low #Args : #Author :Aaron Kili Kisinga #Email :[email protected] #License : GNU GPL-3 ####################################################################################### ## declare mail variables ##email subject subject="Server Memory Status Alert" ##sending mail as from="[email protected]" ## sending mail to to="[email protected]" ## send carbon copy to also_to="[email protected]" ## get total free memory size in megabytes(MB) free=$(free -mt | grep Total | awk '{print $4}') ## check if free memory is less or equals to 100MB if [[ "$free" -le 100 ]]; then ## get top processes consuming system memory and save to temporary file ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txt file=/tmp/top_proccesses_consuming_memory.txt ## send email if system memory is running low echo -e "Warning, server memory is running low!\n\nFree memory: $free MB" | mailx -a "$file" -s "$subject" -r "$from" -c "$to" "$also_to" fi exit 0
After creating your script /etc/scripts/alertmemory.sh, make it executable and symlink to cron.hourly.
# chmod +x /etc/scripts/alertmemory.sh # ln -s -t /etc/cron.hourly/alertmemory.sh /etc/scripts/alertmemory.sh
This means that the above script will be run after every 1 hour as long as the server is running.
Tip: You can test if it is working as intended, set the bench mark value a little high to easily trigger an email to be sent, and specify a small interval of about 5 minutes.
Then keep on checking from the command line using the free command provided in the script. Once you confirm that it is working, define the actual values you would like to use.
Below is a screenshot showing a sample alert email.
That’s all! In this article, we explained how to use shell script to send alert emails to system administrators in case server memory (RAM) is running low. You can share any thoughts relating to this topic, with us via the feedback form below.
Hi,
I need a script for an Email Alert when the Productin MQ configuration changes to Active-Active instead of Active-Passive.
Help Appreciated.
Hi,
I need a script for when the server got shutdown and startup we get an email alert.
Kindly help with anyone.
I have to send a mail if the utilization percentage is greater than 90.
can you please tell me how?
Hi, I have the same requirement, can someone help me with this?
I want to get an email if one of my processors is using 90 percent of memory from the allocated heap memory. For example, I have a process Headset: -Xmx4096m (heap memory). As soon as it reaches 90% of the allocated heap memory, I need to get an email.
Hi Aaron,
Does this script supports even DL’s or only restricted to email.
@uday
It only supports email but you can modify it to suit your use case.
This script will just be executed every hour, not when your Memory is running low…
Create a job it would check component if server down to send the email admin or someone
mailx doesn’t send the file?
@Ronnie
Try to troubleshoot to find out why mailx is not sending the file, or configure another mail client in the script.
Thank You, Aaron! An idea for next article: how to create separate php log file and send it by email as attachment every day.
@Jaro
Welcome, thanks for giving us a topic to research about, i will surely work on this.
Instead of placing the script in /etc, place it in /usr/local/bin or $HOME/bin as a user or /usr/local/sbin if you are root.
@houghi
Thanks for the recommendation, we will consider doing this in future guides.
This:
is a really ugly solution. If you want to filter a text and do something with the filtered values in awk/perl/etc, don’t use grep!
Try something like this, please:
@HZ
Oh yeah. Many thanks for this useful tip.
“a really ugly solution”? Please, don’t exaggerate. Let’s say instead you don’t like it. Your solution is a bit cleaner (I’ll give you that) but not much else.