I have a perl script using Inotify2 to watch a directory. I have it almost working the way I want it in that it sends an e-mail notification when a file is modified in the directory. This is an sftp directory for incoming files. When a large file is received, the watch will call a sub routine to perform actions (along with sending an e-mail) that the file is modified. However, since the file is being sent via sftp, it is constantly being modified during the send, causing the user to receive multiple e-mails.
Is there any way to set a timeout after it has noticed that a file is being modified? Is this the way to go, or should I try something else?
You didn’t mention you did look into sleep allready. I can also not study your script nor your design, so I can not decide if this sleep (of mabe a few seconds) influences things negative. Just wanted to point you to its existance.
#print "$name was accessed
" if $event->IN_ACCESS; #print "events for $name have been lost
" if $event->IN_Q_OVERFLOW;
$event->w->cancel;
}) or die “watch creation failed: $!”;
}
1 while $inotify->poll;
sub perform_actions {
my ($name, $dir, $action) = @_;
#DO MULTIPLE ACTIONS HERE. NOT RELEVANT FOR POST
e-mail notification if the size is > 0
my $date = date;
my $cmd = qq(echo “The $name file has been $action on machine” | mailx -s “A new file has been received on $date” user_to_email);
system($cmd) if -s $name;
}
}
It seems to me that your SFTP daemon is the appropriate place to trigger the notification action. Are their options to the daemon that would allow for this?
I can look into options to the daemon. I am using scponlyc as a shell for the sftp user and I think this automatically calls the sftp-server program as it chroots.