[NTLUG:Discuss] Setting up a watch folder

Michael Barnes barnmichael at gmail.com
Thu Jan 15 08:49:59 CST 2009


On Wed, Jan 14, 2009 at 3:53 PM, Daniel Hauck <daniel at yacg.com> wrote:
> This is definitely something I could cobble together on my own but I
> have decided to pose the question here because the answers might be more
> generally educational and because the answers might be better than
> anything I would have considered.
>
> Here's the project:
>
> Users on a small network (3 users total) are using an SME server to host
> their files, their logins and that sort of thing.  They also print
> through this server.  They have an HP600 plotter at their site as well.
>
> Recently, for whatever reason, their ability to send plot files to their
> plotter has broken.  Probably some Windows update has thrown a
> monkey-wrench into the works, but I don't care to fix Windows problems
> and it occurred to me that all we need to do is set up a watch folder
> and let people drag and drop their plot files into the folder and let
> the server do the job of queuing the file and spooling to the plotter.
>
> I have already successfully sent a plot file to the plotter from command
> line, so that functionality is simple and works.  So now I just want to
> set up a "watch folder" that will respond to plot files being present by
> sending them to the plotter and then deleting the file.
>
> I imagine setting up a cron job to run every minute and when it sees a
> file in the folder, it would launch a script that does the work.
>
> How would "you" do it?  I like bash scripting, but perl would be fine...
> or frankly, whatever you like best.
>
> This is the sort of process that could be adapted for all sorts of tasks
> such as image processing or anything else you would like to let a server
> handle in response to a file being copied to a folder.
>
>


I had a situation similar where I had to move files between two
networks.  I used a dual NIC machine, one NIC on each network.  I then
mounted a share on each network.  I watched the share on one side and
when a file was dropped into it, the script moved it to the
appropriate folder on the other network.

Anyhow, you might be able to hack this perl script to meet your needs.
 Its not elegant and I know there are other ways to do it, but I am a
simple person.  Maybe parts of this will help.

Michael

#!/usr/bin/perl -w


# Scripts to monitor xfer folder and move the files to
# the proper folders
#
#
#       by Michael Barnes
#
#

use File::Copy;
use POSIX qw(setsid);

chdir '/'                 or die "Can't chdir to /: $!";
umask 0;
 open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
 open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
 open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
unless(my $pid = fork()) {
exit if $pid;
setsid                    or die "Can't start a new session: $!";

$listpath = "/var/log/";
$listfile = "dropbox.log";


while(1) {
        sleep(35);
        $date = `date +%Y%m%d%H%M`;
        chomp ($date);

        @filelist = glob "/mnt/xfer/*";
        @logs = grep  (/txt|TXT/, @filelist);   # cull out text files
        @mp3 = grep  (/mp3|MP3/, @filelist);    # cull out mp3 files
        @wav = grep  (/wav|WAV/, @filelist);    # cull out wav files

        foreach $_ (@logs) {
                push @logfile,$_;
                }
open (OUTPUT,  ">>$listpath$listfile");
foreach $logfile (@logfile) {
                print OUTPUT "$date$logfile\n";
                }
close (OUTPUT);

        foreach $logfile (@logfile) {
                $newlogfile = $logfile;
                $newlogfile =~ s/\/mnt\/xfer/\/mnt\/Data\/logs/;
                move ($logfile, $newlogfile) or die "Holy moly we got
us a log error here hoss!";
                }
        undef (@logfile);

        foreach $_ (@mp3) {
                push @mp3file,$_;
                }
open (OUTPUT,  ">>$listpath$listfile");
foreach $mp3file (@mp3file) {
                print OUTPUT "$date$mp3file\n";
                }
close (OUTPUT);

        foreach $mp3file (@mp3file) {
                $newmp3file = $mp3file;
                $newmp3file =~ s/\/mnt\/xfer/\/mnt\/Audio\/dropbox\/mp3/;
                move ($mp3file, $newmp3file) or die "Holy moly we got
us an mp3 error here hoss!";
                }
        undef (@mp3file);

        foreach $_ (@wav) {
                push @wavfile,$_;
                }
open (OUTPUT,  ">>$listpath$listfile");
foreach $wavfile (@wavfile) {
                print OUTPUT "$date$wavfile\n";
                }
close (OUTPUT);


        foreach $wavfile (@wavfile) {
                $newwavfile = $wavfile;
                $newwavfile =~ s/\/mnt\/xfer/\/mnt\/Audio\/dropbox\/wav/;
                move ($wavfile, $newwavfile) or die "Holy moly we got
us a wav error here hoss!";
                }
        undef (@wavfile);
        }
}



More information about the Discuss mailing list