Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Unable to search for failed pattern in the files created in a directory.

by venky4289 (Novice)
on Jun 21, 2013 at 09:06 UTC ( [id://1040118]=perlquestion: print w/replies, xml ) Need Help??

venky4289 has asked for the wisdom of the Perl Monks concerning the following question:

Actually, I need to find the file when it gets created in the directory and i need to go inside the file and search for the failed pattern and i need to print the lines where that pattern is matched. Note: Here the file created is a log file so it will take some time to finish its creation. So now i need to search the file after its totally populated with the data.

#!/usr/bin/perl use strict; use warnings; use Carp; use File::Monitor; $| = 1; my $monitor = File::Monitor->new; push @ARGV, '.' unless @ARGV; while ( my $obj = shift ) { $monitor->watch( { name => $obj, recurse => 1 } ); } sub sendEmail { my ($to, $from, $subject, $message) = @_; my $sendmail = '/usr/lib/sendmail'; open(MAIL, "|$sendmail -oi -t"); print MAIL "From: $from\n"; print MAIL "To: $to\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; close(MAIL); } while ( 1 ) { my $val; sleep 1; my $time = localtime(); for my $change ( $monitor->scan ) { print $change->name, " changed\n"; my @val = $change->files_created; $val = join( ' ', @val ); if ( $val ) { printf( " %s\n", $val ); sendEmail("venkatesh.reddy\@us.fijistu.com","venkatesh.redd +y\@us.fijistu.com","file creation","$val created at $time.." ); + } xxxx : if($val =~ /(run.*\.log)$/){ print "log name: $1\n"; open(my $fh, "$1"); while(<$fh>) { if($_=~/failed/) { print "$_\n"; } } close($fh); } } }

But With the above code i am able to monitor the files created, but not able to search for the pattern required in the file created, so i am coming out of the loop in the starting of the log file creation, So now i need to stay in the loop till the file gets created and i need to search for log .

Can any one advise me in this issue, Thanks in Advance.

  • Comment on Unable to search for failed pattern in the files created in a directory.
  • Download Code

Replies are listed 'Best First'.
Re: Unable to search for failed pattern in the files created in a directory.
by Happy-the-monk (Canon) on Jun 21, 2013 at 10:03 UTC

    With the above code i am able to monitor the files created, but not able to search for the pattern required in the file created, so i am coming out of the loop in the starting of the log file creation, So now i need to stay in the loop till the file gets created and i need to search for log

    Your problem is a bit hard to understand (at least for me), maybe that's why nobody has answered this yet.

    I don't know File::Monitor yet, but what I understand is that it notifies you about creation of a file, but not about adding content to it?

    I think you need to find a way to monitor the adding of content, which may happen some time in between but definitely when it is being closed, i.e. done writing for good.

    If there was no module for that task on cpan (I haven't been looking for that yet), you might

    • put all newly created files you care about in a hash with their size as values (see stat) and
    • look for changes periodically.

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: Unable to search for failed pattern in the files created in a directory.
by Anonymous Monk on Jun 22, 2013 at 01:27 UTC

    Hi,

    If you are on a *nix box, have a look at fuser.

    J.C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1040118]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found