Operations 8 min read

Automate Linux Email Alerts and Memory Monitoring with msmtp, mutt, and Bash

This guide walks you through installing the msmtp email client, configuring mutt to send mail, using the free command to monitor system memory, creating Bash scripts for memory and swap alerts, and scheduling these checks with cron on a Linux server.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Automate Linux Email Alerts and Memory Monitoring with msmtp, mutt, and Bash

1. Install and configure msmtp

Download the msmtp source package, extract it, and compile:

# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make install

Create the configuration file ~/.msmtprc (set permissions to 600) and a log file:

# vim ~/.msmtprc
account default
host 126.com
from [email protected]
auth login
user test
password 123456
logfile ~/.msmtp.log
# chmod 600 ~/.msmtprc
# touch ~/.msmtp.log

2. Configure mutt to use msmtp

Edit ~/.muttrc to point mutt’s sendmail to the msmtp binary and set sender details:

# vim ~/.muttrc
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="memory"
set [email protected]
set envelope_from=yes
set rfc2047_parameters=yes
set charset="utf-8"

3. Test email sending

Send a test message with subject and attachment:

# echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt [email protected]

4. Monitor system memory with free

Use the free -m command to view memory usage in megabytes, then extract specific values:

# free -m
# free -m | grep Mem | awk '{print $4}'   # physical free
# free -m | grep - | awk '{print $4}'   # buffers/cache free
# free -m | grep Swap | awk '{print $4}' # swap free

5. Create a Bash script to log memory statistics

# vim /scripts/free-mem.sh
#!/bin/bash
# Record timestamp
date >> /scripts/date-time.txt
# Log free values
echo Mem-free: `free -m | grep Mem | awk '{print $4}'`M >> /scripts/mem-free.txt
echo buffers/cache-free: `free -m | grep - | awk '{print $4}'`M >> /scripts/buffers-free.txt
echo Swap-free: `free -m | grep Swap | awk '{print $4}'`M >> /scripts/swap-free.txt
# Combine into one file
paste /scripts/date-time.txt /scripts/mem-free.txt /scripts/buffers-free.txt /scripts/swap-free.txt > /scripts/freemem.txt
# chmod a+x /scripts/free-mem.sh

6. Email the memory log

# vim /scripts/sendmail-mem.sh
#!/bin/bash
# Get server IP
IP=`ifconfig eth0 | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "`
# Get current date
today=`date -d "0 day" +%Y年%m月%d日`
# Send email with attachment
echo "这是${IP}服务器${today}的内存监控报告,请下载附件。" \
  | mutt -s "${IP}服务器${today}内存监控报告" -a /scripts/freemem.txt [email protected]
# chmod a+x /scripts/sendmail-mem.sh

7. Monitor swap usage and send alerts

# vim /scripts/swap-warning.sh
#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "`
swap_total=`free -m | grep Swap | awk '{print $2}'`
swap_free=`free -m | grep Swap | awk '{print $4}'`
swap_used=`free -m | grep Swap | awk '{print $3}'`
if ((swap_used != 0)); then
  swap_per=$(echo "scale=2; $swap_free/$swap_total" | bc)
  swap_warn=0.20
  swap_now=$(expr $swap_per ">" $swap_warn)
  if (( $swap_now == 0 )); then
    echo "${IP}服务器swap交换分区只剩下 ${swap_free}M 未使用,剩余不足20%,使用率已超过80%,请及时处理。" \
      | mutt -s "${IP}服务器内存告警" [email protected]
  fi
fi
# chmod a+x /scripts/swap-warning.sh

8. Schedule the scripts with cron

# crontab -e
*/10 * * * * /scripts/free-mem.sh
*/10 * * * * /scripts/swap-warning.sh
0 8 * * * /scripts/sendmail-mem.sh
# service crond restart

These steps provide a complete, automated solution for sending email alerts and continuously monitoring memory and swap usage on a Linux server.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxEmailmsmtpmutt
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.