Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 :
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 

share|improve this question

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
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

Your Answer


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.