How to process updates from a continuous process in Perl

I am trying to track log files in Perl on Fedora, but unfortunately Fedora uses journalctlbinary log files to read, which I cannot parse directly. This, in my opinion, means that I can read Fedora log files by calling journalctl.

I tried to use it IO::Pipe, but the problem is that it $p->reader(..)waits until the journalctl --followoutput is written (which will never be, since it --followlooks like tail -F), and then it allows me to print everything that is not what I want. I would like to be able to set a callback function that will be called every time a new line is printed in the process channel so that I can parse / process every new log event.

use IO::Pipe;

my $p = IO::Pipe->new();
$p->reader("journalctl --follow"); #Waits for process to exit

while (<$p>) {
  print;
}
+4
source share
2 answers

I guess that journalctlworks like tail -f. If this is correct, the downtime openshould complete the task:

use Fcntl; # Import SEEK_CUR

my $pid = open my $fh, '|-', 'journalctl --follow'
    or die "Error $! starting journalctl";
while (kill 0, $pid) {
    while (<$fh>) {
        print $_; # Print log line
    }
    sleep 1; # Wait some time for new lines to appear
    seek($fh,0,SEEK_CUR); # Reset EOF
}

open : http://perldoc.perl.org/functions/open.html

seek reset EOF: http://perldoc.perl.org/functions/seek.html reset <$fh> EOF, script .

kill 0,$pid , , open, .

sleep 1 usleep Time::HiRes select undef,undef,undef,$fractional_seconds;, .

AnyEvent AnyEvent::Handle.

Update:

use POSIX ":sys_wait_h"; waitpid $pid, WNOHANG) ( ) journalctl:

while (kill(0, $pid) and waitpid($pid, WNOHANG) != $pid) {

, , , $pid ($$), journalctl .

+3

journalctl, IO::Pipe ,

use strict;
use warnings 'all';

open my $follow_fh, '-|', 'journalctl --follow' or die $!;

print while <$follow_fh>;
0

All Articles