Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have written a shell script that reads the updating logs and sends an email notification when there are OOM using do while loop.

I am able to get the email notification for OOM errors but every time a duplicate PID is getting generated for the email sent which is creating a huge no. of duplicate pids while I am grepping for that particular process/ Admin server (weblogic). PFB my script, is there any way I can avoid the duplicate PIDs and can get only parent PID while doing a grep.

#!/bin/sh
# Script to read the updating log file and send mail for any errors : LogCheck.sh
# SET VARIABLES
logfile=BEA_HOME/SERVERS/Admin.log
pattern="java.lang.OutOfMemoryError: Java heap space" 

#Read each line as it gets updating to the log file
tail -fn0 $logfile | while read line ; do 

    #check each line for our pattern
    echo "$line" | grep -i "$pattern"

    #Perform the below action if a line matches with our pattern
    if [ $? = 0 ]; 

    then
        #Send an email 
        echo "Found an error: $line" | mailx -s "please check the error" emailID 
    fi

done
share|improve this question

2 Answers 2

ps wp PID|grep PID

You ca do it without the grep - you will find with grep you remove the header..

share|improve this answer

You can use uniq or sort -u:

echo "$line" | grep -i "$pattern" | uniq
share|improve this answer
    
Hi, Thanks you for the suggestion. I tried using these options but when I use either uniq or sort -u , I am not receiving the email notification for message "java.lang.OutOfMemoryError: Java heap space", getting the notifications for other messages. Also when I do a grep i.e ps -ef | grep AdminServer - along with parent pid it is producing some other PIDs like below.. –  user174783 Jul 17 '13 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.