How to Install and Configure Mailx for Automated Log Monitoring on Linux
This guide walks you through installing Mailx on a Linux server, fixing compilation issues, configuring SMTP settings, testing email delivery, and building a keyword‑based log‑monitoring script that triggers alerts via email.
The article explains a practical solution for monitoring Linux server logs and sending email alerts when specific keywords appear.
1. Install Mailx
Download the Mailx source package and extract it:
tar -jxf mailx-12.4.tar.bz2 -C /usr/src/ && cd /usr/src/mailx-12.4/Compile and install: make install UCBINSTALL=/usr/bin/install If compilation fails, apply the provided patch:
patch -p0 < mailx-12.4-openssl_1.0.0_build_fix-1.patchRe‑run the install command after patching.
2. Verify Installation
Check the installed version:
mailx -V3. Configure Mailx
Edit /etc/nail.rc and add the following lines at the end (replace placeholders with your actual values):
set [email protected]
set smtp=smtp.example.com
set smtp-auth-user=your_user
set smtp-auth-password=your_password
set smtp-auth=loginSave the file; the configuration is now ready.
4. Test Email Sending
Send a test message from the command line:
echo '邮件正文' | mailx -v -s "邮件标题" [email protected]If the command succeeds, Mailx is correctly configured.
5. Develop Keyword Monitoring Scripts
Two shell scripts are used:
CheckLogs.sh – monitors a log file, remembers the last read line, and scans new lines for configured keywords.
SendMail.sh – sends an email; can be customized for subject, body, and recipients.
A configuration directory ( conf) holds a file named chklist that lists the keywords to watch for.
When CheckLogs.sh runs for the first time, it records the current line count. Subsequent runs read only the newly appended lines; if any of the keywords appear, the script invokes SendMail.sh to issue an alert.
Testing steps include inserting a test keyword (e.g., error) into the monitored log, running the script, and confirming that the keyword is captured and an email is sent. The article also demonstrates monitoring multiple keywords and controlling the check frequency.
After completing these tests, the log‑monitoring and email‑alert system is fully operational.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
