Database Administration (MOSC)

MOSC Banner

shell script to grep for errors in a log file and then email the results.

edited Feb 13, 2024 10:26PM in Database Administration (MOSC) 2 commentsAnswered ✓

#!/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")

Howdy, Stranger!

Log In

To view full details, sign in to My Oracle Support Community.

Register

Don't have a My Oracle Support Community account? Click here to get started.

Category Leaderboard

Top contributors this month

New to My Oracle Support Community? Visit our Welcome Center

MOSC Help Center