BASH
Real-time Monitoring of Apache/Nginx Error Logs for Critical Issues
Monitor web server error logs (Apache or Nginx) in real-time, filtering for critical error messages to quickly identify and respond to production issues.
#!/bin/bash
# Configuration
# Adjust this to your web server's error log path
# For Apache: /var/log/apache2/error.log or /var/log/httpd/error_log
# For Nginx: /var/log/nginx/error.log
LOG_FILE="/var/log/nginx/error.log"
# Keywords to look for (case-insensitive)
# Add or remove keywords based on what you consider critical
KEYWORDS="error|fail|critical|warn|denied|timeout|segmentation fault|upstream prematurely closed connection|permission denied|no such file or directory"
# Check if log file exists
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Log file not found at $LOG_FILE"
exit 1
fi
echo "Monitoring $LOG_FILE for critical keywords: ($KEYWORDS)"
echo "Press Ctrl+C to stop monitoring."
# Use tail -f to follow new entries, and grep to filter
# --line-buffered: ensures grep outputs lines as soon as they are received
# -i: case-insensitive search
# --color=auto: highlights the matched keywords
tail -f "$LOG_FILE" | grep --line-buffered -i -E "$KEYWORDS" --color=auto
How it works: This script provides real-time monitoring of web server error logs (e.g., Apache or Nginx). It uses `tail -f` to continuously output new lines appended to the log file and pipes this output to `grep`. `grep` then filters these lines, displaying only those that contain any of the specified `KEYWORDS`. The `-i` option makes the search case-insensitive, and `--color=auto` highlights the matched terms for better visibility. This is an invaluable tool for quickly detecting and responding to critical issues in a production environment.