LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to check the string (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-check-the-string-760684/)

elainelaw 10-08-2009 10:21 PM

Script to check the string
 
In my system , there is a program will generate a log to the text file , this program will run times per day , can advise if I want to check the text file , if the text file have a string "Uuable to run program" , then send mail to me , can advise what can i do ? thx

chrism01 10-08-2009 10:39 PM

You could try logwatch http://linux.die.net/man/8/logwatch.
The main question is whether you want to do this in batches eg once per day, in which case logwatch should do nicely. If you want real-time, I don't know if logwatch can do that.
If not, you could possibly amend the script that attempts to run the invalid prog to email you immediately.
Another option is to find or write a prog to continuously tail the logfile, looking for such errors eg the perl module http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm is designed to do just that.

elainelaw 10-09-2009 03:40 AM

thx reply ,

instead of use logwatch , as I tried to setup logwatch ,but it is too difficult to me , is there any method ( script ) do it ? please advise .

lutusp 10-09-2009 03:57 AM

Quote:

Originally Posted by elainelaw (Post 3713223)
thx reply ,

instead of use logwatch , as I tried to setup logwatch ,but it is too difficult to me , is there any method ( script ) do it ? please advise .

Write a script that does something like this:

1. Read recent log entries and check for the desired text:

Code:

if tail -n 50 < logfilename | grep -q "Unable to run program"
then
  # do something here
fi

(choose how many lines you need to read -- 50 is just an example)

2. In the above "do something here" position, send an e-mail to a specified recipient:

Code:

echo “program so-and-so failed to run at `date`” | mail -s “Program Launch Failure” user@domain.com
3. Run the sequence above periodically from cron.

It's really easy to set up something like this.

chrism01 10-11-2009 08:47 PM

The fiddly bit will be ensuring you don't pickup the same error more than once ie in subsequent checks. You need to know what line the last known msg was on, hence the Perl soln I mentioned.
If you make a note of the line num in a temp file, that could work.


All times are GMT -5. The time now is 06:41 PM.