For this purpose, you want the text of the error message as much as the timestamp, to know if the error is the same or different. That should be relatively simple. The question would be how to enforce the time window.

Two approaches suggest themselves to me. First, store the timestamp and error message in a file. The next time your script runs, read the timestamp from that file and compare to the current time. This will involve learning how to convert strings into a scalar (or object) representing time. There are CPAN modules to help with this; Time::Piece is the first one I found using a simple search. This would be useful skill to have, and in my opinion worth the effort.

However, a simpler approach would be to write just the error message out to a file (so you can compare it to the next message), and treat the create date of the file as the timestamp. It should be reasonably close to the actual error timestamp (worst case plus the running time interval of your script). And, you can use the -M operator which returns the age of the file in days to determine if it has been 30 minutes or not since you last saw that error.

use strict; use warnings; my $window = 30.0 / 1440; # 30 minutes my $error_file = 'error.txt'; my $error_message = shift; chomp($error_message); if (! -e $error_file) { # generate warning and then write_error_file(); } else { chomp(my $old_error = `cat $error_file`); # two conditions warrant a warning # a) either a new type of error or b) it has been # more than 30 minutes since we last saw this one if (($old_error ne $error_message) || (-M $error_file > $window)) { # generate warning and then write_error_file(); } } sub write_error_file { my $ofh; print "New error $error_message\n"; open $ofh,'>',$error_file; print $ofh $error_message; close $ofh; }
1 Peter 4:10

In reply to Re^5: Simple awk question by GotToBTru
in thread Simple awk question by czah7

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.