It looks like you want something that will actually tell you if the OS is aware that the file is being changed or has been modified. I don't know of anything like that, but it's not hard to roll your own file-checker, one that simply 'gets the job done'. I've put my solution below. It monitors all the files in the directory for changes in size and when a file hasn't changed its size since the last iteration, it deems it done and sftp's it. It also monitors files that have been sftp'd already, in case a new file arrives with the name of an old one.

Note that this code compiles, but I didn't check to see if the logic actually works as advertised (-:

#!/usr/bin/perl use warnings; use strict; my $dir_to_monitor = 'your/directory/here'; chdir $dir_to_monitor; # We'll wait for 60 seconds between invocations my $sleep_time = 60; # We deem a file 'unchanging' if its size hasn't changed since # the last time we checked. Thus, these hashes hold the file # sizes from our last time through the loop: my %unchanging_file_sizes; # Already been sftp'd elsewhere my %changing_file_sizes; # Files that are potentially growing # Holds the state of the loop. This is set to zero when the file # named 'stop_sftp_checking' is found. my $still_running = 1; while ($still_running) { # Check all the files in the directory FILE: foreach my $filename (glob '*') { # Ignore the file if it hasn't changed next FILE if (-s $filename == $unchanging_file_sizes{$filename}); # Check files that were changing the last time around to see # if they're done changing. if (-s $filename == $changing_file_sizes{$filename}) { sftp_file($filename); delete $changing_file_sizes{$filename}; $unchanging_file_sizes{$filename} = -s $filename; next FILE; } # At this point we can be sure that either the file is new # or the file size has changed since we last checked. Either # way, make sure we check it on our next go 'round the loop: delete $unchanging_file_sizes{$filename}; $changing_file_sizes{$filename} = -s $filename; # Finally, our exit condition. If we find a file named # 'stop_sftp_checking' then quit after handling the last file. $still_running = 0 if $filename eq 'stop_sftp_checking'; } # Wait and repeat sleep $sleep_time; }

Note that I assumed you have a function named sftp_file() that will perform the needed sftp file copy command.

This has really, really obvious holes in it, but you should be able to patch them if you care. It waits for a minute between checks, so if you need a fast response, it won't work for you. Also, it assumes that you will never receive a file named 'stop_sftp_checking'; though it's unlikely you'll ever receive such a file by accident, it could pose a security hole if constant up-time is critical and you're facing the world. There are better ways to handle the stopping condition such as monitoring a control file outside the directory. I assume you have some notion of security and how to secure your system.

Remember, Perl is fun, and hackish solutions are usually all that you really need to fix the problem and move on. Hope that helps!


In reply to Re: Monitoring directory for new files by pdltiger
in thread Monitoring directory for new files by ranciid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.