Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This is overkill, but I think there are some pieces here that you can use. I used temporary and semaphore files.
use strict; use warnings; use Fcntl qw(:flock); use Time::localtime; $| = 1; my $logFile = "mainLog.LOG"; my $SEMAPHORE = '~'. $logFile . '.lck'; my $was_awaiting = 0; sub WRITE_TO_LOGFILE(); sub READ_LOGFILE_FOR_SENDMAIL (); sub IS_FILE_LOCKED($); sub TIMESTAMP (); ################################################################### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################################################### print "\nWRITE TO LOGFILE .... \n"; WRITE_TO_LOGFILE (); print "\nWRITE TO LOGFILE .... [DONE]\n"; print "\nREAD LOGFILE FOR SENDMAIL .... \n"; READ_LOGFILE_FOR_SENDMAIL (); print "\nREAD LOGFILE FOR SENDMAIL .... [DONE]\n"; ################################################################### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################################################### sub WRITE_TO_LOGFILE () { open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; print "[Semaphore open]\n"; while ( IS_FILE_LOCKED($SEMAPHORE) ) { print "\r\t[Awaiting Semaphore lock]"; $was_awaiting = 1; } if ($was_awaiting) { print "\n"; $was_awaiting = 0; } print "\t[Semaphore lock] \n"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; print "\t\t[Logfile open]\n"; open (FH, ">>$logFile") or die "Can't open $logFile: $!"; print FH "I have written from PID ($$)::[".TIMESTAMP() +."]::PHY-4-EXCESSIVE_ERRORS\n"; close FH; print "\t\t[Logfile closed]\n"; print "\t\tGoing to sleep...\n"; sleep 10; print "\t\tWoken up...\n"; close S; print "[Semaphore unlock]\n"; print "[Semaphore closed]\n"; unlink($SEMAPHORE); } sub READ_LOGFILE_FOR_SENDMAIL () { open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; print "[Semaphore open]\n"; while ( IS_FILE_LOCKED($SEMAPHORE) ) { print "\r\t[Awaiting Semaphore lock]"; $was_awaiting = 1; } if ($was_awaiting) { print "\n"; $was_awaiting = 0; } print "\t[Semaphore lock] \n"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; print "\t\t[Logfile open]\n"; open (FH, "<$logFile") or die "Can't open $logFile: $!"; open (FHT, ">", '~'."$logFile") or die "Can't open temp $l +ogFile: $!"; while(<FH>) { chomp; if ($_ =~ m/PHY-4-EXCESSIVE_ERRORS/) { ## SENDMAIL() $_ =~ s/PHY-4-EXCESSIVE_ERRORS/PHY_4_EXCESSIVE +-ERRORS -- SENDMAIL SENT/g; print FHT "$_\n"; } else { print FHT "$_\n"; } } close FH; close FHT; unlink($logFile); ## DELETE PREVIOUS LOGFILE rename('~'."$logFile",$logFile); ## RENAME TEMP LOG +FILE TO REPLACE PREVIOUS LOG FILE print "\t\t[Logfile closed]\n"; close S; print "[Semaphore unlock]\n"; print "[Semaphore closed]\n"; unlink($SEMAPHORE); } sub IS_FILE_LOCKED ($) { my $theFile; my $theRC; ($theFile) = @_; $theRC = open(my $HANDLE, ">>", $theFile); $theRC = flock($HANDLE, LOCK_EX|LOCK_NB); close($HANDLE); return !$theRC; } sub TIMESTAMP () { my $t = localtime; return sprintf( "%04d-%02d-%02d_%02d-%02d-%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } __END__ -RUNTIME- C:\monks>perl loggie.pl WRITE TO LOGFILE .... [Semaphore open] [Semaphore lock] [Logfile open] [Logfile closed] Going to sleep... Woken up... [Semaphore unlock] [Semaphore closed] WRITE TO LOGFILE .... [DONE] READ LOGFILE FOR SENDMAIL .... [Semaphore open] [Semaphore lock] [Logfile open] [Logfile closed] [Semaphore unlock] [Semaphore closed] READ LOGFILE FOR SENDMAIL .... [DONE] C:\monks> - Log File - I have written from PID (7892)::[2013-11-19_11-52-41]::PHY_4_EXCES +SIVE-ERRORS -- SENDMAIL SENT I have written from PID (7452)::[2013-11-19_11-53-33]::PHY_4_EXCES +SIVE-ERRORS -- SENDMAIL SENT

In reply to Re: Search logs with crontab by VincentK
in thread Search logs with crontab by hmb104

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (11)
    As of 2015-07-28 08:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (254 votes), past polls