#!/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 my $thr1 = threads->create(\&jobinotifydetect); # start inotify in seperate thread $thr1->detach(); #main while (1) { sleep 1; { # here comes main > for now remove every second a file out of stack lock (@jobstack); shift (@jobstack); } } sub jobinotifydetect { my $smbroot = "/media/lrfp/"; my $sharedfolder = "jobs/"; my $path = $smbroot . $sharedfolder ; my $jobinotify; my $file; $jobinotify = new Linux::Inotify2 or die "unable to create new inotify object: $!"; $jobinotify->watch ("$path", IN_MOVED_TO | IN_CLOSE_WRITE, sub { my $e = shift; my $file = $e->name; my $t=$e->fullname; #only do for files with non-zero size if ( -s $t ) { if ( $e->IN_MOVED_TO || $e->IN_CLOSE_WRITE ) { # don't push onto stack if already exist if (!( grep( /^$file/, @jobstack ) ) ){ { lock (@jobstack); push (@jobstack,$file); }}}} # } }); 1 while $jobinotify->poll; }