Operations 8 min read

Automate Linux Memory & Swap Monitoring with msmtp, mutt, and Cron

This guide walks you through installing the msmtp mail client, configuring mutt to send emails, using the free command to gather memory statistics, writing Bash scripts to log and email those metrics, setting up swap‑usage alerts, and scheduling everything with cron for continuous Linux system monitoring.

ITPUB
ITPUB
ITPUB
Automate Linux Memory & Swap Monitoring with msmtp, mutt, and Cron

1. Install msmtp (a lightweight mail client)

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

2. Create the msmtp configuration file (~/.msmtprc) and restrict its permissions

# 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

3. Configure mutt to use msmtp as its sendmail program

# 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"

4. Test email sending with mutt

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

5. Monitor system memory using the free command # free -m Key values can be extracted with:

Physical free memory: # free -m | grep Mem | awk '{print $4}' Buffers free: # free -m | grep - | awk '{print $4}' Swap free: # free -m | grep Swap | awk '{print $4}' 6. Bash script to collect memory data ( /scripts/free-mem.sh )

#!/bin/bash
# Record timestamp
date >> /scripts/date-time.txt
# Record physical free memory
echo Mem-free: `free -m | grep Mem | awk '{print $4}'`M >> /scripts/mem-free.txt
# Record buffers free
echo buffers/cache-free: `free -m | grep - | awk '{print $4}'`M >> /scripts/buffers-free.txt
# Record swap free
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

7. Bash script to email the memory report ( /scripts/sendmail-mem.sh )

#!/bin/bash
# Server IP
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
# 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

8. Bash script to warn when swap usage exceeds 80% ( /scripts/swap-warning.sh )

#!/bin/bash
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -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=0`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

9. 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 fully automated solution for collecting memory and swap statistics on a Linux server, emailing daily reports, and sending immediate alerts when swap usage becomes critical.

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.

LinuxcronMemory Monitoringshell scriptmsmtpmuttswap alert
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.