#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use Linux::Inotify2; my @jobstack :shared ; # shared array for filenames # spawn thread sub # start inotify in separate thread my $thr1 = threads->create(\&jobinotifydetect); $thr1->detach(); #main while (1) { sleep 1; { # here comes main > for now remove every second a file out of stack lock (@jobstack); # I can't splice shared arrays so I copy it onto a nonshared one > later I need splice to pull out specific files 1st # now it's just a pop replacement my @temp=@jobstack; my $t = scalar (@temp); splice (@temp,$t-1,1) if ($t ); @jobstack=@temp; } } sub jobinotifydetect { my $smbroot = "/media/lrfp/"; my $sharedfolder = "jobs/"; my $path = $smbroot . $sharedfolder ; my $jobinotify; my $file; my $count; my $tt; $jobinotify = new Linux::Inotify2 or die "unable to create new inotify object: $!"; # add watcher $jobinotify->watch ("$path", IN_MOVED_TO | IN_MODIFY | IN_CREATE, sub { my $e = shift; if ( $e->IN_MOVED_TO || $e->IN_MODIFY || $e->IN_CREATE ) { # wait for file locks to be removed > when data is still written by server my $file = $e->name; my $t=$e->fullname; my $exec = `smbstatus | grep "EXCLUSIVE" | grep "$sharedfolder" | grep "$file"`; if (!($exec)) { # check if filesize <> 0,don't if file has zero length if ( -s $t ) { { # lock @jobstack while I push filename onto it lock (@jobstack); # don't push onto stack if already exist if (!( grep( /^$file/, @jobstack ) ) ){ push (@jobstack,$file); $tt=scalar (@jobstack); print "$count \t $file \t $tt \n"; $count = 0; }}} } else { # count how much times routine gets called just out of curiosity, I'll remove this later $count +=1 ; } } }); 1 while $jobinotify->poll; }