shell script to grep for errors in a log file and then email the results.
#!/bin/bash
# Log file path
LOG_FILE="/path/to/your/log_file.log"
# Error pattern (case-sensitive)
ERROR_PATTERN="ERROR|FAIL"
# Email recipient address
RECIPIENT="your_email@example.com"
# Email subject prefix
SUBJECT_PREFIX="Errors found in $LOG_FILE"
# Mail sending method (replace with your preferred method)
# Option 1: Using mailx (requires mailx installed)
MAIL_COMMAND="mailx -s"
# Option 2: Using sendmail (replace "your_smtp_server" with your server)
# MAIL_COMMAND="sendmail -t -H To: $RECIPIENT -H Subject: $SUBJECT_PREFIX"
# Check if any errors exist
if grep -q "$ERROR_PATTERN" "$LOG_FILE"; then
# Get only the lines containing errors
ERRORS=$(grep "$ERROR_PATTERN" "$LOG_FILE")