openSUSE Forums > Programming/Scripting » Perl Inotify Script Help

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-Sep-2009, 08:59
Puzzled Penguin
 
Join Date: Mar 2009
Posts: 28
pwright2 hasn't been rated much yet
Default Perl Inotify Script Help

Hi all,

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?
Reply With Quote
  #2 (permalink)  
Old 29-Sep-2009, 10:18
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,910
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Perl Inotify Script Help

Can't you call sleep from Perl? (not being a Perl programmer myself).
__________________
Henk van Velden
Reply With Quote
  #3 (permalink)  
Old 29-Sep-2009, 11:44
Puzzled Penguin
 
Join Date: Mar 2009
Posts: 28
pwright2 hasn't been rated much yet
Default Re: Perl Inotify Script Help

If I use sleep, will it suspend the process? This would not be desired in case any other files are arriving during the nap time .

I was thinking of this, but wanted to steer away from this because of my comment above.
Reply With Quote
  #4 (permalink)  
Old 29-Sep-2009, 12:11
hcvv's Avatar
Wise Penguin
 
Join Date: Jun 2008
Location: Netherlands
Posts: 1,910
hcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enoughhcvv 's reputation will be famous soon enough
Default Re: Perl Inotify Script Help

Yes, it will (see man sleep).

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.
__________________
Henk van Velden
Reply With Quote
  #5 (permalink)  
Old 29-Sep-2009, 13:15
malcolmlewis's Avatar
Global Moderator
 
Join Date: Jun 2008
Location: Podunk
Posts: 4,701
malcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputationmalcolmlewis has great reputation
Default Re: Perl Inotify Script Help

Quote:
Originally Posted by hcvv
Yes, it will (see -man sleep-).

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.
Hi
Aside from Henk's comments I've used http://kodu.neti.ee/~risto/sec/ in
the past for these sorts of activities.

--
Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.29-0.1-default
up 2 days 19:54, 2 users, load average: 0.16, 0.29, 0.42
GPU GeForce 8600 GTS Silent - CUDA Driver Version: 190.18

Reply With Quote
  #6 (permalink)  
Old 29-Sep-2009, 17:03
Puzzled Penguin
 
Join Date: Mar 2009
Posts: 28
pwright2 hasn't been rated much yet
Default Re: Perl Inotify Script Help

I have posted my script below for any comments:
---------------------------------------

#!/usr/bin/perl

use Linux::Inotify2;

close STDIN;
close STDOUT;
close STDERR;

sub perform_actions;

$dir_base = "/srv/sftp/sftpuser/incoming/";
my @directories = qw(dir1 dir2);

#create new object
my $inotify = new Linux::Inotify2 or die "unable to create new inotify obejct: $!";

foreach my $directory (@directories)
{
#add watchers
#print "Adding watch for " . $dir_base.$directory;
$inotify->watch($dir_base.$directory, IN_CREATE | IN_MODIFY, sub { my $event = shift;

my $name = $event->fullname;

if($event->IN_CREATE)
{
perform_actions($name, $dir_base.$directory, "created");
}
elsif($event->IN_MODIFY)
{
perform_actions($name, $dir_base.$directory, "modified");
}

#print "$name was accessed\n" if $event->IN_ACCESS;
#print "events for $name have been lost\n" 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;
}
}
Reply With Quote
  #7 (permalink)  
Old 30-Sep-2009, 22:54
Puzzled Penguin
 
Join Date: Sep 2009
Posts: 8
craverii hasn't been rated much yet
Default Re: Perl Inotify Script Help

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?
Reply With Quote
  #8 (permalink)  
Old 01-Oct-2009, 11:21
Puzzled Penguin
 
Join Date: Mar 2009
Posts: 28
pwright2 hasn't been rated much yet
Default Re: Perl Inotify Script Help

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.

Anybody have experience with this?
Reply With Quote
Reply

Bookmarks

Tags
inotify, perl, watch


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2