script to grep latest outofmemory string from the logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to grep latest outofmemory string from the logs
# 1  
Old 12-16-2010
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.
Code:
f=catalina.out

var=`tail -10 $f | grep -i outofmemory | sed -n '$p' | sed -n -e "s/.*\(OutOfMemoryError\).*/\1/p"`

I need improvement in the above command in such a way that it should always grepped for latest outofmmeory message even if logs are not updating. can anyone help me on this?

Moderator's Comments:
Mod Comment Having so many posts already, you should be familiar using code tags - you got PM with a guide.

Last edited by zaxxon; 12-16-2010 at 06:07 AM..
# 2  
Old 12-16-2010
Follwoing should give the latest line (last line in log) having "outofmemory" string, which can be piped to other command as further processing needed.
Code:
 
grep -i "outofmemory" | tail -1

If it doesn't help, give OutOfMemoryError message example pls and expected output
# 3  
Old 12-16-2010
Hello Anurag,

Thanks a lot for your response. My system logs will contain out of memory message in below format.
Quote:
bash-3.00$ grep -i outofmemory SystemOut.log_bak14122010
[12/14/10 14:43:03:732 CET] 00000041 servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet springDispatcherServlet in application ccrtool. Exception created : org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space
Caused by: java.lang.OutOfMemoryError: Java heap space
[12/14/10 14:46:15:086 CET] 00000041 webapp E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[springDispatcherServlet]: java.lang.OutOfMemoryError: Java heap space
My requirement is script should search entire file for the outofmemory error message and if the error is latest then it should send the alert mail. Please let me know if you have further query.
# 4  
Old 12-16-2010
Code:
lastError=""
while true
do
    error=`grep -i outofmemoryerror file | tail -1 | sed 's/.*:\(.*\)/$1/g'`
    if [ "$error" != "$lastError" ] ; then
        mail -s "SUBJECT CONTAINING $error" abc@abc.com
        lastError="$error"
    else
        echo "No new error"
    fi
    sleep 10
done

R0H0N
# 5  
Old 12-16-2010
Hello Rohan,

Thanks for your quick reply.

I ran each command in the script.

Code:
error=`grep -i outofmemoryerror file | tail -1`

I got the below output.

Quote:
bash-3.00$ echo $error
[12/14/10 14:46:15:086 CET] 00000041 webapp E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[springDispatcherServlet]: java.lang.OutOfMemoryError: Java heap space
Then i ran below command i got the following output.

Code:
echo $error | sed 's/.*:\(.*\)/$1/g'
$1

Can you please explain me the sed section of the code and why i received $1 as output.

Thanks in advance.
# 6  
Old 12-16-2010
Code:
echo "$error" | sed 's/.*:(.*)$/\1/g'

R0H0N
# 7  
Old 12-16-2010
Hi, I am getting following error.
Code:
 bash-3.00$ echo $error | sed 's/.*:(.*)$/\1/g' sed: command garbled: s/.*:(.*)$/\1/g bash-3.00$

Moderator's Comments:
Mod Comment If you continue not to use code tags, you may find yourself unable to post. Why not use them?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. UNIX for Beginners Questions & Answers

Grep a pattern & Email from latest logs

MyLOG: 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... (3 Replies)
Discussion started by: vivekn
3 Replies

3. Shell Programming and Scripting

Search string or words in logs without using Grep

I'm in need of some kind of script that will search for a string in each logfile in a directory but we don't want to use GREP. GREP seems to use up to much of our memory causing the server to use up a lot of swap space. Our log files are bigger than 500M on a daily basis. We lately started... (8 Replies)
Discussion started by: senormarquez
8 Replies

4. Shell Programming and Scripting

Script to grep logs for Errors

Hi Guys, I want to write a script which can grep the logs (server.log) from a file for Error String and output to a other file. Problems: How to know about the errors only between the current restart and not in previous as server.log has earlier restarts also? thanks for the help! Much... (5 Replies)
Discussion started by: ankur328
5 Replies

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

6. Shell Programming and Scripting

shell script to grep 500 error messages from access logs

Hello Team, I need help to improve my script which is used to grep 500 error messages in the logs. I am using following logic in the script to grep 500 error messages in the logs. var1=`awk '$9 == "500"' access_log | tail -1` The above logic is not useful if logs are not getting... (1 Reply)
Discussion started by: coolguyamy
1 Replies

7. Shell Programming and Scripting

script to grep outofmemory message in logs

I have prepare script to grep for outofmemory messages in the logs. I need help in modifying script. I have implemented small logic. The outofmemory messages form six logs will store in variables. var1=`grep -i outofmemory $tomcat1logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`... (6 Replies)
Discussion started by: coolguyamy
6 Replies

8. Shell Programming and Scripting

Grep string from logs of last 1 hour on files of 2 different servers and calculate count

Hi, I am trying to grep a particular string from the files of 2 different servers without copying and calculate the total count of its occurence on both files. File structure is same on both servers and for reference as follows: 27-Aug-2010... (4 Replies)
Discussion started by: poweroflinux
4 Replies

9. UNIX for Dummies Questions & Answers

grep -c script counting string twice instead of once?

I tried this script to get a count of the occurrence of a string in files. I have multiple files in one directory I will use this on. All the filenames begin "invALL.06" The script works, except it counts twice for every one instance of 'Invoice Total'. If there are 5 occurences of 'Invoice... (2 Replies)
Discussion started by: scarletsupra
2 Replies

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