Operations 4 min read

How to Install Mailx on Linux and Build a Keyword Log Monitoring Script

This guide walks you through installing the Mailx email client on a Linux server, applying necessary patches, configuring SMTP settings, testing email delivery, and creating a shell script that monitors log files for keywords and automatically sends alert emails when matches are detected.

Open Source Linux
Open Source Linux
Open Source Linux
How to Install Mailx on Linux and Build a Keyword Log Monitoring Script

1. Install Mail Service

Extract the Mailx source package, compile and install it, then apply a patch to fix OpenSSL build errors.

tar -jxf mailx-12.4.tar.bz2 -C /usr/src/ && cd /usr/src/mailx-12.4/
make install UCBINSTALL=/usr/bin/install
patch -p0 < mailx-12.4-openssl_1.0.0_build_fix-1.patch
make install UCBINSTALL=/usr/bin/install

Verify the installation with mailx -V.

Configure /etc/nail.rc with your SMTP credentials:

set from=
set smtp=
set smtp-auth-user=
set smtp-auth-password=
set smtp-auth=login

Test sending an email:

echo '邮件正文'|mailx -v -s "邮件标题" [email protected]

2. Keyword Monitoring Script Development

Create CheckLogs.sh to monitor log files for specified keywords and trigger SendMail.sh when matches are found. Store configuration files in a conf directory (e.g., chklist).

The first execution records the current line count of the target log. Subsequent runs read only newly appended lines and extract keywords such as “error”. Detected keywords cause the mail alert script to run.

Multiple keywords can be monitored, and frequency control can be adjusted as demonstrated in the screenshots.

After successful testing, the system continuously watches logs and sends email alerts for critical events.

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.

LinuxLog Monitoringemail alertsmailx
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.