in reply to Re^4: trying to implement file tail with regular expression
in thread trying to implement file tail with regular expression

Try this. Note I have used a module to read the ini file.

#!perl use strict; use File::Tail; use Config::Tiny; #die "Usage: $0 <app_root_dir>" unless @ARGV == 1; my $app_root_dir = shift; =head1 content of param.ini DIAGFILE = diagfile HOSTNAME1 = hostname1 HOSTNAME2 = hostname2 =cut my $Config = Config::Tiny->read( "$app_root_dir/config/param.ini" ) or die "$!"; my $ref = tie *DIAG,"File::Tail",( name => $Config->{'_'}{'DIAGFILE'}, interval => 1, # wait at start maxinterval => 10, # change to suit adjustafter => 10 # change to suit ); my $restart; while (my $line = <DIAG>) { if ($line =~ /is higher than 5 seconds/) { # restart only if more than 60 seconds since last restart if (time() - $restart > 60) { run_command('stop',1); run_command('stop',2); run_command('start',1); run_command('start',2); $restart = time(); } } } # execute start/stop sub run_command { my ($action,$n) = @_; my $host = $Config->{_}{'HOSTNAME'.$n}; my $cmd = join ' ','sc',"\\\\$host",$action,'AudioSrv', ">$app_root_dir\\logs\\${action}ServiceOutput${n}.txt"; #system($cmd); print "$cmd\n"; # replace with system after testing }
poj

Replies are listed 'Best First'.
Re^6: trying to implement file tail with regular expression
by mkhayat (Initiate) on Feb 17, 2016 at 05:29 UTC

    again thanks for your support. I tried your code but I faced the same issue. when its reaching while (my $line = <DIAG>) its stop. I am using Eclipse for step by step troubleshooting. I dont understand why this is happening. without using the file tail class it works fine and I was able to read from the file!!!!!! do you have any other idea please?

      Perhaps try it without tie.

      #my $ref = tie *DIAG,"File::Tail",( # name => $Config->{'_'}{'DIAGFILE'}, # interval => 1, # wait at start # maxinterval => 10, # change to suit # adjustafter => 10 # change to suit #); my $fh = File::Tail->new( name => $Config->{'_'}{'DIAGFILE'}, interval => 1, # wait at start maxinterval => 10, # change to suit adjustafter => 10 # change to suit ); my $restart; #while (my $line = <DIAG>) { while (my $line = $fh->read) { print $line; if ($line =~ /is higher than 5 seconds/) { . .

      This was the log generator I use to test the script

      #!perl use strict; my $n = 3; my $s = 10; print "Log generator running .. $n events every $s secs .. \n"; open OUT,'>','c:/temp/diag.log' or die "$!"; select OUT; $|=1; while (1){ print STDOUT scalar localtime."\n"; for (1..$n){ print scalar localtime,"- abc is higher than 5 seconds xyz\n"; } print scalar localtime."- some other log message $_\n" for 0...rand( +10); sleep($s); }
      poj

        same thing once it reach while (my $line = $fh->read) it stop. :(