BASH
Monitor Web Server Error Logs in Real-time
A bash script to continuously monitor Apache or Nginx error logs, displaying new error entries as they occur. Essential for quick identification and debugging of issues on a live web server.
#!/bin/bash
LOG_FILE="/var/log/nginx/error.log" # Change to your Nginx or Apache error log path
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Log file '$LOG_FILE' not found." >&2
exit 1
fi
echo "Monitoring '$LOG_FILE' for new error entries... (Press Ctrl+C to stop)"
tail -f "$LOG_FILE" | grep -iE "error|warn|crit|fail"
How it works: This script utilizes `tail -f` to follow the end of a specified log file, continuously outputting new lines. The output is then piped to `grep -iE "error|warn|crit|fail"`, which filters for lines containing common error-related keywords (case-insensitive). This provides a real-time stream of important log messages, making it easy for web developers to spot issues immediately after changes or during incident response.