Problem with log file script


 
Thread Tools Search this Thread
Operating Systems Solaris Problem with log file script
# 1  
Old 09-18-2012
Problem with log file script

Hi,

I have a log file that i'd like to monitor for a string. Now the log file rolls over everytime the server is restarted, so i'm doing a reverse list and then reading the latest log file.

So for the code, the log file is 'test1.txt'.

I check for the string and pipe to black hole (> /dev/null)....and if this is true, I do the same again but this time I mail a message to the support team.

If this is false (||) I email to support team that no entry is found.

So far my code is:

Code:
#!/usr/bin/bash

set -- $(ls -r test1.txt) ; grep -i 'STRING_I_NEED_TO_FIND' $1 > /dev/null &&
set -- $(ls -r test1.txt) ; grep -i 'STRING_I_NEED_TO_FIND' $1 | 
mailx -r server@org.com -s "Error Message found on server $HOSTNAME, please investigate" my_email@whatever.com 
|| mailx -r server@org.com -s  "Error not found" my_email@whatever.com

when i have entered dummy error messages into my dummy file (test1.txt) i get them emailed to me.

however, my problem is that even if my dummy 'test1.txt' is blank, i still get a message saying that the error has been found and to investigate.

any help greatly appreciated.

thanks
# 2  
Old 09-18-2012
Try this:

Code:
#!/usr/bin/bash

set -- $(ls -r test1.txt) 
grep -iq 'STRING_I_NEED_TO_FIND' $1 
if [ $? -eq 0 ] ; then
   mailx -r server@org.com -s "Error Message found on server $HOSTNAME, please investigate" my_email@whatever.com 
else   
  mailx -r server@org.com -s  "Error not found" my_email@whatever.com
fi

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 09-19-2012
Quote:
Originally Posted by jim mcnamara
Try this:

Code:
#!/usr/bin/bash

set -- $(ls -r test1.txt) 
grep -iq 'STRING_I_NEED_TO_FIND' $1 
if [ $? -eq 0 ] ; then
   mailx -r server@org.com -s "Error Message found on server $HOSTNAME, please investigate" my_email@whatever.com 
else   
  mailx -r server@org.com -s  "Error not found" my_email@whatever.com
fi

hi this doesnt seem to work for me. also i cannot use the 'q' in grep.

thanks anyhow
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

Problem - create log file

I need to create shell script (check system ) to display the output to the console and write these output to a log file at the same time. My shell script is like that Main() { .... .... } MainIt takes about 30s to display all the output to the console. Then I put this command to the... (4 Replies)
Discussion started by: bobochacha29
4 Replies

3. Shell Programming and Scripting

Script to read a log file and run 2nd script if the dates match

# cat /tmp/checkdate.log SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 17 22:49:00 2012 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production FIRST_TIME NEXT_TIME... (1 Reply)
Discussion started by: SarwalR
1 Replies

4. HP-UX

Script to monitor /var/opt/resmon/log/event.log file

AM in need of some plugin/script that can monitor HP-UX file "/var/opt/resmon/log/event.log" . Have written a scrip in sh shell that is working fine for syslog.log and mail.log as having standard format, have interrogated that to Nagios and is working as I required . But same script failed to... (3 Replies)
Discussion started by: Shirishlnx
3 Replies

5. Shell Programming and Scripting

Problem - Log File

Hi, I am in trouble ... again :wall: I formating a log file as .txt from Unix (solaris 9) and then I compress it with gzip and I use uuencode and send it in an email. gzip -c ${fic_report} | uuencode Report_File.gz > ${attachment_file} my problem is when I received the mail with the... (6 Replies)
Discussion started by: Aswex
6 Replies

6. Shell Programming and Scripting

Problem with Log File

Hi, I have written a bash shell script. In order to create a log file when I run the script, as well as print the output to the terminal, I call it in the following manner: my_shell_script 2>&1 | tee my_log_fileThis seems to work well. However, I have put some statements in the script to... (2 Replies)
Discussion started by: msb65
2 Replies

7. Shell Programming and Scripting

log file problem

Hi, I have a problem with my logfile. I am redirecting my output to log file and when iam viewing as text doc it is looking with some nonsense characters...please do help me on this .thank you. here is my logfile which it is showing different behaviour: spawn spawn minicom Welcome... (6 Replies)
Discussion started by: vanid
6 Replies

8. Shell Programming and Scripting

Problem in redirecting ftp msgs to a log file

Hi all.. The following set of statements is used in a shell script to ftp a file to a remote machine I want to redirect the ftp messages thrown by the first ftp statement to a log file. As you can see there is a logic downstream to decide if the ftp was a success or not. But i am not... (5 Replies)
Discussion started by: hareeshkumaru
5 Replies

9. Shell Programming and Scripting

Log file problem

I want to transfer files from ABC server to XYZ server in which i am successful. I would like to create a log file also by the name of current date. For some reason my script gives me an error on my scp -p command and it says LOCAL_LOG_FILE: not found and REMOTE_LOG_FILE not found. Any clues whats... (9 Replies)
Discussion started by: chris1234
9 Replies

10. HP-UX

rc.log problem about autostart script

On HP-UX, we add some our autostart scripts in the system script under /sbin/rc*.d directory. The output of application is redirected to /dev/null in our script,but once the application has been startup with the OS starting,its ouput is redirected to the /etc/rc.log finally and cause the rc.log... (2 Replies)
Discussion started by: Frank2004
2 Replies
Login or Register to Ask a Question