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.
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/installpatch -p0 < mailx-12.4-openssl_1.0.0_build_fix-1.patchmake install UCBINSTALL=/usr/bin/installVerify 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=loginTest 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
