Dearest Monks,

I need to send files to a process whenever new files become available in a set of directories. The files can appear in the directories as quickly as every second to every minute. Each file should only be passed to the process (pqinsert) once and no files should be missed (both of these conditions must be satisfied). I wrote a Perl script to continuously poll the directories at a given interval, pass the file names to the external process, then move the processed files to an archive sub-directory.

Can you tell me if the code below is the most efficient to solve this problem and if it isn't give suggestions for improvement? Thank you so much!

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use File::Copy; $path = "/mnt/ldmdata/"; @site_array = {"karx", "kdlh", "kfsd", "kmpx", "kmvx", "kwbc"}; $poll_time = 20; # # of sec between polls of all specified directories for (;;) { foreach $site (@site_array) { $file_dir = $path . $site; $archive_dir = $file_dir . "/archive"; mkdir "$archive_dir", 0755 unless -d "$archive_dir"; opendir(FILE, $file_dir) || die "Cannot open $file_dir"; @files = readdir(FILE); closedir(FILE); if(@files) { foreach $file (@files) { pqinsert $file_dir . $file; move($file_dir . $file, $archive_dir . $file); } } } sleep $poll_time; }

UPDATE: I wanted to share a package I found which is built on "inotify" to perform monitoring/action tasks like this. It looks pretty robust:

iWatch


In reply to Continuously polling multiple directories for file transfer? by pktrain

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.