bash - Is it possible to use perl/ajax to read a file line by line while the file is written by another program? -
it similar question being asked herehow read file line line using ajax request while file written other program using java? have file populated command line outputs generated remote machine. every time gets written in file, want use perl (or javascript quite dubious it) capture , display being written in opened webpage. ideally each line should shown in html written in file way how generated in terminal.
my difficulty not sure how should polling - detecting being written in file - , how can capture line @ real time.
that being said, possibility have thought of change script on remote machine , dump terminal output div of website. avoid writing, reading , realtime polling not sure if possible?
ignoring ajax second, perl program use file::tail.
with ajax, you'd have reimplement file::tail. following basic version:
#!/usr/bin/perl use strict; use warnings; use cgi qw( ); use fcntl qw( seek_set ); use text::csv_xs qw( decode_json encode_json ); $qfn = '...'; { $cgi = cgi->new(); $request = decode_json( $cgi->param('postdata') || '{}' ); $offset = $request->{offset} || 0; open(my $fh, '<:raw', $qfn) or die("can't open \"$qfn\": $!\n"); seek($fh, $offset, seek_set) or die("can't seek: $!\n"); $data = ''; while (1) { $rv = sysread($fh, $data, 64*1024, length($data)); die("can't read \"$qfn\": $!\n") if !defined($rv); last if !$rv; } $offset .= length($data); print($cgi->header('application/json')); print(encode_json({ data => $data, offset => $offset, })); }
Comments
Post a Comment