BASH

Monitor Apache/Nginx Logs for Specific Errors

A bash script to continuously tail and filter web server access or error logs (Apache/Nginx) for specific keywords like "ERROR", "500", or "denied", providing real-time monitoring.

#!/bin/bash

# --- Configuration ---
LOG_FILE="/var/log/nginx/error.log" # Path to the web server log file
KEYWORDS="ERROR|500|failed|denied" # Keywords to search for (case-insensitive)

# Check if log file exists
if [ ! -f "${LOG_FILE}" ]; then
    echo "Error: Log file '${LOG_FILE}' not found!"
    exit 1
fi

echo "Monitoring log file: ${LOG_FILE} for keywords: ${KEYWORDS}"
echo "Press Ctrl+C to stop."

# Tail the log file and filter for keywords
tail -f "${LOG_FILE}" | grep -Ei --color=always "${KEYWORDS}"
How it works: This script helps web developers monitor web server logs in real-time. It uses `tail -f` to continuously output new lines from a specified log file (e.g., Nginx or Apache error logs). The output is then piped to `grep -Ei --color=always` to filter for specified keywords (like "ERROR" or "500") in a case-insensitive manner and highlight them for easier visibility.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs