Archive of UserLand's first discussion group, started October 5, 1998.

tail -f in a web page

Author:Brent Simmons
Posted:11/5/1999; 3:30:35 PM
Topic:A question for Apache gurus
Msg #:12764 (In response to 12763)
Prev/Next:12763 / 12765

Question for Perl scripters:

I want a web page that allows me to view the last n lines of the Apache log. This web page would auto-refresh every 10 seconds or so. It would be kind of like tail -f in a web page -- but also kind of like the WebSTAR console, which is the point, now that www.scripting.com isn't running in WebSTAR anymore.

Anyway, my method for getting the last n lines from a log file seems inefficient, as it requires looping through the entire log file twice. Can this be improved on? I'm a total newbie Perl scripter.

Here's my script:

#!/usr/bin/perl -w

$lines = 0; $filename = "/var/log/httpd/access_log";

#Count the number of lines

open (FILE, $filename) or die "Can't open filename '$filename': $!"; while (sysread FILE, $buffer, 4096) { $lines+= ($buffer =~ tr/n//); } close FILE;

print $lines; print " lines. \n";

$currline = 0; $lastlines = ""; $numlastlines = 10;

open (FILE, $filename) or die "Can't open filename '$filename': $!";

#Get the last 10 lines in a string.

while () {

if ($currline == $lines) { last; }

if ($currline >= ($lines - $numlastlines)) { $lastlines = $lastlines . $_; }

$currline++; }

close FILE;

print $lastlines;

print "\n";




There are responses to this message:


This page was archived on 6/13/2001; 4:53:22 PM.

© Copyright 1998-2001 UserLand Software, Inc.