Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-02-2004, 05:58 PM   #1 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Perl - tailing a file

So I'm looking to create some utilities to watch various logs/etc and perform actions when certain criteria is found in the files. I have a basic example working, but if the log is truncated a certain way, the script doesn't detect the file change. Any ideas?

Code:
cat /dev/null > logfile
After something like that it doesn't catch the first addition.

I also ran across the File::Tail module, but would rather have all the code self contained instead of having external requirements..

Code:
use File::stat;
use IO::Handle;

$| = 1;

$file = '/tmp/foo1';

open(FH, $file) or die "$!\n";

for (;;) {

   while(<FH>) { print }

   sleep 1;

   if (stat(*FH)->nlink == 0) {
      print "=== new FH\n";
      close(FH);
      open(FH, $file) or die "$!\n";
      sleep 5;
   }
   else {
      seek(FH, 0, 1);
   }

#   print "=== end for pos=" . tell(FH) . "\n";
}

close(FH);
I was also planning on watching the inode and filesize (for shrinking) and if they change, to close the FH and reopen. Any cool ideas or should I just give up and use the module?

-r
idx is offline   Reply With Quote
Old 11-04-2004, 12:00 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
It would appear that you already have some external requirements: File::Stat and IO::Handle. I do not deny that doing everything by hand is a fun rich experience to learn from, however, I do believe that if you are already using these modules: what is the harm in adding another one?

If you decide that you still want to do it the fun way, just say so. We are here to help.

I also like to do things the hard way and tend to always implement stuff myself for personal projects.

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 11-04-2004, 07:30 PM   #3 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Not sure how well/poor those modules are supported, but they seemed to be in every perl build I tested this on. (freebsd, linux and aix.. each on a different perl version) The main issue with adding the other module is ease of distribution to numerous systems without installing a bunch of stuff. The main kicker is that File::Tail requires Time::HiRes which needs to be compiled. Can't just do a simple use lib '/foo'; and include the module as part of my code..

ack.. gotta get some sleep.

-r
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:51 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting