Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Open file and print new line that is appended

Open file and print new line that is appended

Open file and print new line that is appended

Need to open a file and if a new line is appended, then need to print it.

Opening the file is no problem, but how does one check if a new line has been recently appended to the log file?

RE: Open file and print new line that is appended


Just read, then wait, then start again :

CODE --> Perl

open F,"</input/file";

while (1) {
  print $_ while <F>;
  sleep 1;

( Neither tail -f has better solution : waits -s ( --sleep-interval ) seconds, then tries to read again. )


RE: Open file and print new line that is appended

From CPAN, File::Tail seems to do what you want. It even recomputes the sleep time dynamically (up to a maximum limit) based on how active the log file is, so it doesn't continually poll the file when nothing much is happening.


"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)

RE: Open file and print new line that is appended

You can use the -M switch to test the file modification time:

CODE --> perl

my $lasttime=0;
my $myinputfile="myinputfile.log";
  if(-M $myinputfile<$lasttime){
    $lasttime=-M "myinputfile";
    open F,$myinputfile;
    print $_ while <F>;
    close F;
  sleep 1;
It is not difficult to add the dynamically calculated sleep time.

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Open file and print new line that is appended


Sorry Franco, but I am afraid that is not really practical :

Quote (man perlfunc)

-M Script start time minus file modification time, in days.
( man perlfunc | -X )

So better keep using the good old stat function.

Regarding your code, reading the whole file after each modification not sounds too good. Also expecting a modification time less than the $lasttime sounds abit back to the future...

The OP not specified the used operating system, so hopefully this not applies, but as I know, on Windows the file time granularity is not really fine and not all changes will be detected promptly.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close