On Feb 12, 2008 9:58 AM, Chas. Owens <[EMAIL PROTECTED]> wrote:
> On Feb 12, 2008 12:38 PM, Michael Barnes <[EMAIL PROTECTED]> wrote:
> snip
> > I'm the new kid and this is a beginners forum, so I welcome all ideas
> > and options.  Forgiving my ignorance, would you mind giving an example
> > of how I would do this with lsof?
> snip
>
> This only works on systems with lsof.  Checking file size change is a
> fairly portable, if inexact, way of checking to see if a file is still
> being written to.  Of course, lsof also has problems: what if the file
> is opened, written to, and closed, opened, written to, closed, etc.
> and the lsof runs while the file is closed?
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $lsof = "/usr/sbin/lsof";
> my $file = "/tmp/foo.$$";
> my $pid  = fork;
> die "could not fork" unless defined $pid;
>
> unless ($pid) {
>         #child writes to the file for 5 seconds
>         open my $fh, ">", $file
>                 or die "could not open $file\n";
>         for my $i (1 .. 5) {
>                 print $fh "$i\n";
>                 sleep 1;
>         }
>         exit;
> }
>
> #parent monitors file
> my $file_holder_pid = qx($lsof -t $file);
> chomp($file_holder_pid);
> while ($file_holder_pid) {
>         print "$file is current being held open by $file_holder_pid\n";
>         sleep 1;
>         $file_holder_pid = qx($lsof -t $file);
>         chomp($file_holder_pid);
> }
>

Yes, it does make the script tied down to mostly UNIX/Linux systems. I
assumed once the file is dumped in, it won't be accessed by that same
process so no write close, write close operations on the file.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to