Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Automation with Perl

by Zanooda (Initiate)
on May 17, 2010 at 13:44 UTC ( #840356=perlquestion: print w/ replies, xml ) Need Help??
Zanooda has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have just installed a CentOS 5.5 server, I created a directory named (/PVT), Under /PVT I created three directories (A, B, and C for example), there are also three shell scripts (A1.sh, B1.sh, C1.sh) related to A B and C directories. I need the script A1.sh to run every time a new file/directory is created under directory A at any level, also the script B1 to run every time a new file is created under directory B, and so for C.. Can that be done with Perl and do you recommend using Perl for such a task? If that can be done with Perl please provide an example.. Thanks, Zanooda

Comment on Automation with Perl
Re: Automation with Perl
by Corion (Pope) on May 17, 2010 at 13:49 UTC
Re: Automation with Perl
by rubasov (Friar) on May 17, 2010 at 14:06 UTC
    For tasks like you described my preferred way is to use incron. That's not really a Perl solution, however you can call any program (perl also) from the incrontab, and at least you don't need to write your own daemon. Like Corion's first suggestion above it uses Linux Inotify and gives you a cron-like interface for any filesystem event.
Re: Automation with Perl
by cdarke (Prior) on May 17, 2010 at 15:09 UTC
    Use Linux::Inotify2 (Inotify is obsolete). Here is an example:
    use warnings; use strict; use Linux::Inotify2; my $target = defined $ARGV[0]?$ARGV[0]:'.'; print STDERR "Watching $target...\n"; my $in2 = Linux::Inotify2->new(); die "Inotify2: $!" if (! defined $in2 ); $in2->watch ($target, IN_ALL_EVENTS) or die "watch: $!"; while (1) { for my $e ($in2->read()) { print "$e->{name} ".$e->mask."\n"; translate_event($e); } } sub translate_event { my ($ev) = @_; my $action = $ev->fullname; $action .= " was read" if ($ev->IN_ACCESS); $action .= " opened for writing was closed" if ($ev->IN_CLOSE_WRITE +); $action .= " not opened for writing was closed" if ($ev->IN_CLOSE_N +OWRITE); $action .= " created in watched directory" if ($ev->IN_CREATE); $action .= " deleted from watched directory" if ($ev->IN_DELETE); $action .= "Watched file/directory was itself deleted" if ($ev->IN_ +DELETE_SELF); $action .= " was modified" if ($ev->IN_MODIFY); $action .= "Watched file/directory was itself moved" if ($ev->IN_MO +VE_SELF); $action .= " moved out of watched directory" if ($ev->IN_MOVED_FROM +); $action .= " moved into watched directory" if ($ev->IN_MOVED_TO); $action .= " was opened" if ($ev->IN_OPEN); print "$action\n"; #return $action?$action:' unknown action'; }
    Just run the required script (maybe using system) when you detect the change you are interested in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://840356]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-07-11 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (219 votes), past polls