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.

# Script to read the updating log file and send mail for any errors : LogCheck.sh
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 ]; 

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


You can use uniq or sort -u:

echo "$line" | grep -i "$pattern" | uniq
  • 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
ps wp PID|grep PID

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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