http://qs321.pair.com?node_id=998528

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

Good afternoon dear Monks,

I'm attempting to search logs using tail -f so if the specific string got into the log file we should print the first 3 occurrences and continue the rest of the script, but it is not working. The strings passed as a commandline argument.

I appreciate the your valuable input

use warnings; use strict; my $currentDate = `date +%Y%m%d`; my $logPath = "/dhcp/logs" my $logFile = "{$path}/DHCPD.log.{$currentDate}" my @tail = qx(tail -f -1 $logFile | grep $_); if (@tail) { print @slice = @tail[0 - 2]; last; } else { print "not found"; }

Replies are listed 'Best First'.
Re: Watch log for string (tail -f)
by BrowserUk (Patriarch) on Oct 11, 2012 at 20:19 UTC
    but it is not working

    Is it sleeping on the job, or just not getting out of bed? :)

    Does it hang; print 'not found'; explode in a rain of pink fairies?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      lol, yes it keep going and going and going. Any ideas about how to make it exit once 3 occurrences are cough in the tail -f command?

        tail -f doesn't end until you interrupt it. Try something like this:

        #! perl -slw use strict; my $pid = open TAIL, '-|', 'tail -f theLog' or die $!; my @matches; while( @matched <3 and defined( $_ = <TAIL> ) ) { push @matches, $_ if /the search term/; } kill 2, $pid; ## do something with the 3 lines in @matches.

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Re: Watch log for string (tail -f)
by roboticus (Chancellor) on Oct 11, 2012 at 20:25 UTC

    hmb104:

    "Not working" isn't particularly helpful. What is it doing? Any error messages, etc.?

    Anyway, here's how I process the a log file with tail -f:

    #!/usr/bin/perl use strict; use warnings; open my $FH, 'tail -f t |' or die; while (<$FH>) { chomp; # processing goes here.... }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Watch log for string (tail -f)
by brap (Pilgrim) on Oct 11, 2012 at 20:24 UTC
Re: Watch log for string (tail -f)
by live4tech (Sexton) on Oct 12, 2012 at 11:01 UTC

    I think the tail -f will continue until interrupted as other posters pointed out.

    I was wondering also why you used {$path} in the line my $logFile = "{$path}/DHCPD.log.{$currentDate}" rather than the variable you created, {$logPath}?

    Is this a typo?

      That was a typo and I have fxed it. I'm still not getting any idea abou7t how to do this, and how to break out the tail -f?

      This is what I got so far:

      #!/NMS/perl/ACTIVE/bin/perl -w use warnings; use strict; #variables my $currentDate = `date +%Y%m%d`; my $logPath = "/logs"; my $logFile = "$logPath/log.$currentDate"; my $ipaddress = $ARGV[0]; my @tail = `tail -f $logFile | grep $ipaddress`; #print "Log file: $logFile\n"; #print "IP address: $ipaddress\n"; # script begins if (@tail) { my @lines = $tail[0 - 2]; print @lines; exit(0); } else { print "not found"; exit(1); }

        I think you will need to use the file handle with the while loop as the other posters have suggested. Have you tried this?