http://www.perlmonks.org?node_id=1063361


in reply to Search logs with crontab

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