Linux allows you to write shell scripts that automate tasks and processes. In fact, you can regularly run shell scripts as cronjobs. Shell scripts simplify system administration. They can be used for system backups, updates, installations, etc. You can even use it for monitoring system usage and server load. Almost every system administrator would like to be automatically notified when their server’s memory becomes low. Here is how to create a shell script to send email alert when memory gets low.
Shell Script to Send Email Alert when Memory Gets Low
Here are the steps to create shell script that regularly monitors system memory and sends emails in case it falls below certain threshold.
1. Create Shell Script
Open terminal and run the following command to create shell script file and open it in text editor.
$ sudo vi /home/ubunut/alert.sh
2. Add Shell Script
Add the following lines to this file. Replace from, to and also_to email addresses shown in bold with the from, to and cc email addresses as per your requirement. We have used a memory threshold of 100mb, highlighted in bold below. You can change it as per your requirement.
#!/bin/bash ## declare mail variables ##email subject subject="Server Memory Status Alert" from="server.monitor@example.com" to="admin@example.com" also_to="admin2@example.com" ## 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
Save and close the file. In the above code, we first define different variables required to send email – subject, from, to, cc. Then we calculate the free memory using free command. Since free command gives a lot of information, we use grep & awk commands to extract the exact amount of free memory.
We use if condition to check if free memory is more than 100mb. If that is the case, we use ps command to get the list of processes along with their memory. We pass its output to sort and head to get the top memory consuming processes. We save this output to a temporary file.
We use echo command to compose email message and use mailx command to send the email. In our email, we attach the temporary file that contains list of top memory-consuming processes.
Please make sure you have the permission to create a temporary file /tmp folder. It is required to record the top memory consuming processes on your system. If that’s not the case, you can simply comment out that line by adding # at its beginning, and remove -a “$file” after mailx command to exclude the attachment.
3. Make Shell Script Executable
Run the following command to make your shell script executable.
$ sudo chmod +x /home/ubuntu/alert.sh
4. Create Cronjob
Run the following command that schedules a cron job to run the above script every hour.
$ ln -s -t /etc/cron.hourly/alert.sh /home/ubuntu/alert.sh
5. Test the Script
You can easily test the script by setting memory threshold to a small value such as 10mb instead of 100mb and wait for cronjob to run.
That’s it. In this article, we have learnt how to create shell script to send email alerts in case your server’s memory’s low. You can modify this shell script as per your requirement.
Also read:
Linux Split File Into Multiple Files
How to Schedule Reboot in Linux
How to Redirect Nohup Output to File
How to Install Dependencies with APT
How to Install Dpkg Dependencies Automatically