HOW-TO watch log files with perl/EV for a fixed amount of time(timeout)?

Lars Holowko Lars.Holowko at
Sat May 22 01:38:23 CEST 2010

Thanks Marc,

I had the non-blocking stuff in there and it almost worked.
The problem I had is in that the EV::loop does not return after the
timeout kicks in unless I send another entry to /var/log/messages. Have
no idea where things get stuck.

I tried all kinds of stuff in $timeout

#	$watcher->stop;
#	$timeout->stop;
#	EV::unloop EV::UNLOOP_ALL;
#	$watcher->invoke;
#	EV::unloop;
#	close $fh;

Nothing did what I was hoping for. I assume the forked tail is dangling


On Thursday, May 20, 2010 at 6:17 PM, Marc Lehmann <schmorp at>
>On Thu, May 20, 2010 at 05:54:05PM -0600, Lars Holowko
<Lars.Holowko at> wrote:
>> my $fn = "tail -f /var/log/messages";
>> open my $fh, '-|', $fn or croak "Can't open $fn: $::OS_ERROR";

>starting an external process per file is not quite low-overhead, but
>certainly easy to implement...

>> my $watcher = EV::io $fh, EV::READ, sub {
>>       my ($watcher, $revents) = @_;
>>       warn "yeah, /var/log/messages should now be readable without
>> blocking!\n";

>No, not at all - you confuse events and non-blocking I/O - the fact
>some data is available does not suddenly make I/O non-blocking. For
>that, you need to set your file descriptor to nonblocking (e.g. with
>AnyEvent::Util::fh_nonblocking or a platform-specific ioctl/fcntl).
>However, note that:

>>       print <$fh>;

>Making the fd non-blockign doesn't mean that you get full lines - for
>you have to do your own buffering.

>Either you do that, or you use some wrapper, scuh as AnyEvent::Handle
>(note, this is untested, but should get you going):

>   my $hdl = new AnyEvent::Handle
>      fh => $fh,
>      on_error => sub { ... },
>      on_read => sub {
>         my ($hdl) = @_;
>         $hdl->push_read (line => sub {
>            my ($hdl, $line) = @_;
>            say "some data: $line";
>         });
>   };

The information contained in this transmission may be confidential. Any disclosure, copying, or further distribution of confidential information is not permitted unless such privilege is explicitly granted in writing by Quantum. Quantum reserves the right to have electronic communications, including email and attachments, sent across its networks filtered through anti virus and spam software programs and retain such messages in order to comply with applicable data security and retention requirements. Quantum is not responsible for the proper and complete transmission of the substance of this communication or for any delay in its receipt.

More information about the libev mailing list