Grep a pattern & Email from latest logs


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep a pattern & Email from latest logs
# 1  
Old 11-15-2017
Grep a pattern & Email from latest logs

MyLOG:

Code:
2017/11/12 17:01:54.600 : Error: LPID: 3104680848  WRONG CRITERIA FOUND. tRealBuilder::Generate

Output Required:

If Ke word "WRONG CRITERIA FOUND" in latest log ( logs are regularly generating - real time) mail to us
once mailed wait for 2 hours for second mail.

mail subject like " WRONG CRITERIA FOUND" in Logs.

is it possible through script or any monitoring tool we can use for real time log monitoring.

OS : Red Hat Enterprise Linux Server release 6.8 (Santiago)

i tried script given below:
Code:
tail -fn0 logfile | \
while read line ; do
        echo "$line" | grep "pattern"
        if [ $? = 0 ]
        then
                ... do something ...
        fi
done

but it doesnot fullfill my requirement.

Last edited by Scott; 11-15-2017 at 07:04 AM.. Reason: Code tags
# 2  
Old 11-15-2017
Hello vivekn,

I'm sure it is a good start at this, but what actually goes wrong? Does it fail to send mail, fail to detect the line, or something else?

If the log file is updated a lot, you are also firing up multiple sub-process for each line written to it. Could you do something more economical like:-
Code:
pattern="WRONG CRITERIA FOUND"

tail -fn0 logfile | \
 grep "$pattern" | \
  while read line
  do
      .... do something ...
  done

In bash you could maybe try the neater:-
Code:
pattern="WRONG CRITERIA FOUND"

while read line
do
   ... do something ...
done < <(tail -fn0 logilfe | grep "$pattern")

This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 11-15-2017
log file rotation

there is log file rotation , a new file created in every half n hour with time stamp.
# 4  
Old 11-15-2017
Did you consider the -F (upper case!) option to tail to deal with the log file rotation?
And, your code snippet - although not too logical nor efficient - doesn't seem "wrong". So please answer rbatte1's question - WHAT doesn't do what you want? Did you try the proposals in the thread from which you borrowed your code?
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send email if latest file in a directory is older than 2 hours

I have a objective of Sending email if latest file in a directory(excluding files of sub-dirs) is older than 2 hours. eg : ls -ltr drwx--x--x 2 abcde abc 256 2017-02-07 20:10 Mail -rw-rw-r-- 1 abcde abc 1170 2017-02-24 17:30 test -rw-rw-r-- 1 abcde abc 356 2017-03-09 18:00 xyz.csv... (3 Replies)
Discussion started by: simpltyansh
3 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

Grep a pattern in current date logs

Hello, I need to write one script which should search particular pattern like ABCD in log file name hello.txt only in current date logs. in current directory i have so many past date logs but grep should be applied on current date logs. on daily basis current date logs are in number 30 and... (2 Replies)
Discussion started by: ajju
2 Replies

4. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

5. Shell Programming and Scripting

Email alerts whenever someone logs into server via SSH any user?

Hi all, Thanks in Advance!! I dont know how to start to write script for this process, my requirement is if any user logs into server automatically Admin get mail alert. how is this possible? any one guide me to complete this process. (1 Reply)
Discussion started by: anishkumarv
1 Replies

6. Shell Programming and Scripting

To find latest set of logs among new and old

Hi All I am writing a script which will select the latest logs (which are generated every night via a script) among old one and new. Script generates set of 3 logs each time it runs. Example : log-WedJun082011_bkt1.log log-WedJun082011_bkt2.log log-WedJun082011_bkt3.log I have... (1 Reply)
Discussion started by: ratneshnagori
1 Replies

7. Shell Programming and Scripting

script to grep latest outofmemory string from the logs

I have requirement to prepare script which will grep for latest outofmemory message from the logs. I have used following command to grep the string from the logs,this script is not effective when logs are not getting updated as it will grep for old message. f=catalina.out var=`tail -10 $f |... (17 Replies)
Discussion started by: coolguyamy
17 Replies

8. Shell Programming and Scripting

Grep & EMail "HOW to quit ??? "

Hello All, I am using the below code to grep particular word from file and then emailing it through mail command. the problem is this that when i run the script so it stops and ask me for the mail body then it asks for cc: and then runs. I dont want to give body and cc: address, i just want... (1 Reply)
Discussion started by: wakhan
1 Replies

9. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies
Login or Register to Ask a Question