Perl has a very good way of "grepping". Use the Perl way instead of calling external GREP. This will be much faster.
Handling (2) requires knowledge of your log file which your post doesn't have.
Update:
This is not so good: if (-e $logFile) {}
The log file could perhaps "go away" for a brief time during some pruning or archiving process.
The next steps in your code are not guaranteed to work.
Use open() to get a file handle. Process the results of that file handle. Once you have a valid file handle, what happens in the directory (like rename or even delete) doesn't matter.
I am assuming that your process runs "in the background", a Windows Service or Unix Daemon.
Think about what happens if you monitoring program fails or fails to start? There are a lot of issues.
Update:
So some code (untested) could look like this:
However, the problems I mentioned above still exist. If FAIL appears anywhere in the log file, this will send an email every time that this script runs! If somehow the logfile cannot be opened, same problem, this will send an email every time that this script runs!my $logFile = "/icd/rc/genus22/logs/genus_221/v22.10-d037/lnx86_64_opt +/1/tamper.log"; open (my $fh, '<', $logFile) or reportErr($!); if ( grep{": FAIL"}<$fh>) #FAIL found anywhere in file! { do something... exit or return... } # text "Fail" not found if here do whatever... sub reportErr { my $error_text = shift; do something... }
A working script needs to address both of these issues. I'd need to know more about your application to find the "best" answer. Basically you need some kind of a persistent flag that has the effect of "throttling down" the error emails. If your script is running continuously, you could keep this as a global variable. Another technique could be to use the file system. When sending an email create a file "last_email_sent", you could check the file modification date of that file and don't send another email unless its been say at least one hour? You don't have to put any data in the flag file itself. Its perfectly legal to have a file with 0 bytes in it. However, perhaps you could put something in this file that perhaps references a line number in the log file or perhaps a date/time value associated with the error. Maybe you don't want to keep reporting the same error over and over again? Maybe after handing an error, you just leave the log file alone, remembering that you've already reported the last error and don't report again unless a new error shows up? Anyway what you want to prevent is flooding the Error_email_box with messages.
In reply to Re: how to grep from a log file
by Marshall
in thread how to grep from a log file
by noviceuser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |