BASH
Real-time Log Monitoring for Errors
Continuously monitor a specified log file for the occurrence of error messages or custom keywords, providing real-time alerts for debugging and system health checks.
#!/bin/bash
LOG_FILE=$1
KEYWORDS=${2:-"error|fail|warning|exception|critical|denied"}
if [ -z "$LOG_FILE" ]; then
echo "Usage: $0 <log_file> [\"keyword1|keyword2\"]"
echo "Example: $0 /var/log/nginx/error.log \"404|denied\""
exit 1
fi
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Log file '$LOG_FILE' not found."
exit 1
fi
echo "Monitoring '$LOG_FILE' for keywords: '$KEYWORDS'"
tail -F "$LOG_FILE" | grep --line-buffered -E "$KEYWORDS" --color=always
How it works: This script provides a real-time monitoring tool for log files. It uses `tail -F` to continuously follow the specified log file as new data is written. The output is then piped to `grep` which filters for common error-related keywords (or custom ones provided as an argument) and highlights them with color, making it easy for web developers to spot issues immediately in application or server logs for quick debugging and health checks.