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

I want a script to "wait until specified file is created and need to read the file until unless it reaches a specific line /keyword.

scenario :

I am installing a application , once it is kicked , it will create a log , the cursor should be there until the log is file reaches a specific keyword/sentence, though it is a log I don't know how much time it takes to complete writing the log file

Please help me in this scenario I wrote it like this

sleep WAIT until -f $installLogFile; chdir ($destinationlocation) || die $!; print "Changing directory to $destinationlocation\n"; chdir ('logs') || die $!; my $lastLine= `tail /1 $installLogFile`; chomp($lastLine); my $secondLastLine= `tail /2 $installLogFile`; chomp ($secondLastLine); $secondLastLine=~s/$lastLine//; chomp ($secondLastLine); if ($secondLastLine eq 'SUCCESSFUL') { print "successful\n"; } else { print " was not successful \n"; exit; }
here after the cursor reaches "sleep WAIT until -f $installLogFile;" it is reading file it is going to else part b'coz still log needs to be updated with keyword once it reaches the end of the file SUCCESSFUL which is still need to write in the log file

Replies are listed 'Best First'.
Re: help need to read a listening log file
by gmargo (Hermit) on Nov 26, 2009 at 16:40 UTC

    My understanding of your question is: "How can I monitor an ever-growing log file until some keyword is found?"

    A method (actually 3 methods) is provided by the Perl Cookbook. See the section "Trailing a Growing File" (Section 8.5 in my first edition).

    O'Reilly has kindly provided Chapter 8 on the net: http://oreilly.com/catalog/cookbook/chapter/ch08.html. Scroll down to the "Trailing a Growing File" section.

Re: help need to read a listening log file
by spx2 (Deacon) on Nov 26, 2009 at 16:26 UTC
Re: help need to read a listening log file
by markuhs (Scribe) on Nov 26, 2009 at 08:13 UTC
    Hi, this is the code in <code>-tags...
    #!/usr/bin/perl use strict; use warnings; my $installLogFile = $ARGV[0]; my $destinationlocation = $ARGV[1]; sleep 1 until -f $installLogFile; chdir($destinationlocation) || die $!; print "Changing directory to $destinationlocation\n"; chdir('logs') || die $!; my $lastLine = `tail /1 $installLogFile`; chomp($lastLine); my $secondLastLine = `tail /2 $installLogFile`; chomp($secondLastLine); $secondLastLine =~ s/$lastLine//; chomp($secondLastLine); if($secondLastLine eq 'SUCCESSFUL') { print "successful\n"; } else { print " was not successful \n"; exit; }
      Hi markuhs, thanks for the code , here the thing is I am automating GUI installation of application using win32 GUI module. once I click on install It will generate a log file and it took almost 40 min to 1 hr depending upon system resources. I tried the code provided by U . but it is coming once log is created and going to else coz the word 'SUCCESSFUL' only appear after 40 min , here I just want to tail the application until it reaches SUCCESSFUL , ofcourse if the installation didn't went of well we can't see the msg in log, there is constant keywork "TIME" at the end of the file ... So I want to tail the log file until it got the keyword "TIME" once it reads the keyword "TIME" then I can assume that installation is over either it is SUCCESSFUL or not, could u help in this.