Check if file is being modified by another process

Discussion in 'Perl Misc' started by kyle.halberstam@gmail.com, Jul 4, 2007.

  1. Guest

    Hi,

    I have an application that creates and writes to an output file I need
    to process. I need to process the file when it is completely written
    to. I do not initially know how big the file will be in the end.
    Further, the application does NOT put a write lock on the file while
    it is writing it. because of the buffering, the program wirtes to the
    file in random chunks not continuously. And what is worse, the file
    format itself could vary so there is nothing in the actual file that
    signals the end of it. Everything is on a linux server.

    What's the most efficient way of checking this? - one way is perhaps
    inifinite loop checking mmtime until it is stable for a certain amount
    of time?? I am not sure.

    Any help will be greatly appreciated. Thanks


    Kyle
     
    , Jul 4, 2007
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I have an application that creates and writes to an output file I need
    > to process. I need to process the file when it is completely written
    > to. I do not initially know how big the file will be in the end.
    > Further, the application does NOT put a write lock on the file while
    > it is writing it.


    Start the application from a Perl script that does hold a lock.
    Roughly:

    use Fcntl qw( :flock);

    my $out = shift;
    $^F = 10_000;
    open my $o, '>', $out or die "Can't create '$out': $!";
    flock $o, LOCK_EX;
    exec '/the/application', '-o', $out;

    (Assuming -o sets the output file of the application.)

    Setting $^F makes sure the filehandle isn't closed across exec(),
    see perlvar. If you use system() instead of exec() you don't need
    it.

    Anno
     
    -berlin.de, Jul 4, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page