How to Install and Configure Mailx on Linux for Log Monitoring and Alerts
This guide walks through installing the Mailx email client on a Linux server, applying a patch to fix compilation errors, configuring SMTP settings, testing email delivery, and building a shell script that monitors log files for keyword anomalies and triggers email alerts.
The article explains how to set up a Linux server to monitor program logs and automatically send email alerts when specific keywords appear, using the Mailx utility and custom shell scripts.
1. Install Mailx
Extract the source package and compile it:
tar -jxf mailx-12.4.tar.bz2 -C /usr/src/ && cd /usr/src/mailx-12.4/ make install UCBINSTALL=/usr/bin/installIf compilation fails, download the patch mailx-12.4-openssl_1.0.0_build_fix-1.patch, place it in the source directory, and apply it:
patch -p0 < mailx-12.4-openssl_1.0.0_build_fix-1.patchRe‑run the make command after patching: make install UCBINSTALL=/usr/bin/install Verify the installation:
mailx -V2. Configure Mailx
Edit /etc/nail.rc and add the following SMTP settings at the end of the file:
set [email protected]
set smtp=smtp.example.com
set smtp-auth-user=your_username
set smtp-auth-password=your_password
set smtp-auth=loginSave the file; the screenshot in the original article shows the edited configuration.
3. Test Email Sending
Send a test message from the command line:
echo 'Email body' | mailx -v -s "Email subject" [email protected]Successful output confirms that Mailx can send mail through the configured SMTP server.
4. Develop Keyword Monitoring Scripts
Three components are created: CheckLogs.sh – reads a log file, tracks the last processed line, and searches new lines for configured keywords. SendMail.sh – a customizable script that sends an email using Mailx.
A conf directory containing chklist, the keyword configuration file.
On the first run, CheckLogs.sh records the current line count. Subsequent executions read only the newly appended lines; if a keyword such as error is found, the script invokes SendMail.sh to issue an alert.
Testing steps shown in the article include inserting the keyword error into the log, running the script to capture the keyword, testing multiple keywords, and adjusting the monitoring frequency. Screenshots illustrate each test result.
5. Completion
After confirming email functionality and successful keyword detection, the log‑monitoring solution is ready for production use.
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.
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.)
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.
