Click here to get back home

infinite loop to monitor directories

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
infinite loop to monitor directories mr.vlad.dracula 05-13-2008
Posted by mr.vlad.dracula on May 13, 2008, 2:43 pm
Please log in for more thread options
Do you think you can point me on the right direction?

Desired result: Monitor directories indefinitely, print on screen if a
file found is older than 1 minute. It's supposed to bypass
subdirectories, hidden files/directories as well.

Problem: if a file is there already on any of the directories, it will
print out the file just fine. However, if I delete that file and drop
another one, wait a minute or more, nothing displays. It's not really
going inside the directories after the first time.

Thank you...

#!/usr/bin/perl

use warnings;
use strict;
use File::Find;

my @directories = ("/home/foo/dir1","/home/foo/dir2");
my $MINUTES = 0.0007; #<-1 min expressed in days
my $condition = 1;

#supposedly loops forever
while ($condition == 1){
                 find(\&process, @directories, no_chdir => 1);
}


sub process {

        #only files, not directories. skip . and .. files

if (-f $_ and !/^\./){

if (-M $_ >= $MINUTES){

        print "$File::Find::name\n";

}

}

sleep 1;

}

Posted by A. Sinan Unur on May 13, 2008, 4:50 pm
Please log in for more thread options
mr.vlad.dracula@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
cf92ce7f01f3@w4g2000prd.googlegroups.com:

> Do you think you can point me on the right direction?
>
> Desired result: Monitor directories indefinitely, print on screen if a
> file found is older than 1 minute.
...

> #!/usr/bin/perl
>
> use warnings;
> use strict;
> use File::Find;
>
> my @directories = ("/home/foo/dir1","/home/foo/dir2");
> my $MINUTES = 0.0007; #<-1 min expressed in days


> my $condition = 1;
>
> #supposedly loops forever
> while ($condition == 1){

while ( 1 ) {

would get rid of the unnecessary variable $condition.

> find(\&process, @directories, no_chdir => 1);
> }

They syntax of your call is incorrect. From perldoc File::Find:

find
find(\&wanted, @directories);
find(\%options, @directories);

You would have noticed this if you had used some debugging print
statements:

Can't stat no_chdir: No such file or directory
Can't stat 1: No such file or directory

find( { wanted => \&process, no_chdir => 1 }, @directories );

> sub process {
> #only files, not directories. skip . and .. files
> if (-f $_ and !/^\./){
> if (-M $_ >= $MINUTES){
> print "$File::Find::name\n";

perldoc -f -X

-M Script start time minus file modification time, in days.

Now, the main problem. You have

> my $MINUTES = 0.0007; #<-1 min expressed in days

If you create a new file, -M on that file will return a negative number.
You have assigned to $MINUTES a positive number so the condition

-M >= $MINUTES

will never hold for any file created after the script has been started.

Because of this logic error, I am a bit confused about what you really
want. Assuming you mean what you say about files "older than one
minute", you need to compare to current time not to -M.

From perldoc perlvar:

$BASETIME
$^T The time at which the program began running, in seconds since

So, to find the files older than 60 seconds, you would use the
following:

#!/usr/bin/perl

use warnings;
use strict;
use File::Find;

use constant SECONDS_PER_DAY => 24*60*60;

my @directories = ( "$ENV/Src/Test" );

while ( 1 ) {
find( { wanted => \&process, no_chdir => 1 }, @directories );
sleep 1;
}

sub process {
return if not -f or /^\./;
my $mtime = -M;
my $age = $^T - $mtime * SECONDS_PER_DAY ;

if ( time - $age > 60 ) {
print "$_ : $age\n";
}
}

__END__


--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by dracula on May 13, 2008, 5:18 pm
Please log in for more thread options
Thank you very much. I appreciate your inputs and suggestions.

/dracula


> mr.vlad.drac...@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
> cf92ce7f0...@w4g2000prd.googlegroups.com:
>
> > Do you think you can point me on the right direction?
>
> > Desired result: Monitor directories indefinitely, print on screen if a
> > file found is older than 1 minute.
>
> ...
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > my @directories = ("/home/foo/dir1","/home/foo/dir2");
> > my $MINUTES = 0.0007; #<-1 min expressed in days
> > my $condition = 1;
>
> > #supposedly loops forever
> > while ($condition == 1){
>
> while ( 1 ) {
>
> would get rid of the unnecessary variable $condition.
>
> > find(\&process, @directories, no_chdir => 1);
> > }
>
> They syntax of your call is incorrect. From perldoc File::Find:
>
> find
> find(\&wanted, @directories);
> find(\%options, @directories);
>
> You would have noticed this if you had used some debugging print
> statements:
>
> Can't stat no_chdir: No such file or directory
> Can't stat 1: No such file or directory
>
> find( { wanted => \&process, no_chdir => 1 }, @directories );
>
> > sub process {
> > #only files, not directories. skip . and .. files
> > if (-f $_ and !/^\./){
> > if (-M $_ >= $MINUTES){
> > print "$File::Find::name\n";
>
> perldoc -f -X
>
> -M Script start time minus file modification time, in days.
>
> Now, the main problem. You have
>
> > my $MINUTES = 0.0007; #<-1 min expressed in days
>
> If you create a new file, -M on that file will return a negative number.
> You have assigned to $MINUTES a positive number so the condition
>
> -M >= $MINUTES
>
> will never hold for any file created after the script has been started.
>
> Because of this logic error, I am a bit confused about what you really
> want. Assuming you mean what you say about files "older than one
> minute", you need to compare to current time not to -M.
>
> From perldoc perlvar:
>
> $BASETIME
> $^T The time at which the program began running, in seconds since
>
> So, to find the files older than 60 seconds, you would use the
> following:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
> use File::Find;
>
> use constant SECONDS_PER_DAY => 24*60*60;
>
> my @directories = ( "$ENV/Src/Test" );
>
> while ( 1 ) {
> find( { wanted => \&process, no_chdir => 1 }, @directories );
> sleep 1;
>
> }
>
> sub process {
> return if not -f or /^\./;
> my $mtime = -M;
> my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> if ( time - $age > 60 ) {
> print "$_ : $age\n";
> }
>
> }
>
> __END__
>
> --
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


Posted by dracula on May 13, 2008, 6:09 pm
Please log in for more thread options
I read the documentation for file::find but still a little unclear.

Would this be the only thing I need to skip subdirectories and hidden
files/directories?
find( { wanted => \&process, no_chdir => 1 }, @directories );

If so, then I don't need this line then right?.
return if not -f or /^\./;

please advise and thanks in advance.
:)

<snip>
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV/Src/Test" );
>
> > while ( 1 ) {
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
> > sleep 1;
>
> > }
>
> > sub process {
> > return if not -f or /^\./;
> > my $mtime = -M;
> > my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> > if ( time - $age > 60 ) {
> > print "$_ : $age\n";
> > }
>
> > }

<snip>




> Thank you very much. I appreciate your inputs and suggestions.
>
> /dracula
>
>
> > mr.vlad.drac...@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
> > cf92ce7f0...@w4g2000prd.googlegroups.com:
>
> > > Do you think you can point me on the right direction?
>
> > > Desired result: Monitor directories indefinitely, print on screen if a
> > > file found is older than 1 minute.
>
> > ...
>
> > > #!/usr/bin/perl
>
> > > use warnings;
> > > use strict;
> > > use File::Find;
>
> > > my @directories = ("/home/foo/dir1","/home/foo/dir2");
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
> > > my $condition = 1;
>
> > > #supposedly loops forever
> > > while ($condition == 1){
>
> > while ( 1 ) {
>
> > would get rid of the unnecessary variable $condition.
>
> > > find(\&process, @directories, no_chdir => 1);
> > > }
>
> > They syntax of your call is incorrect. From perldoc File::Find:
>
> > find
> > find(\&wanted, @directories);
> > find(\%options, @directories);
>
> > You would have noticed this if you had used some debugging print
> > statements:
>
> > Can't stat no_chdir: No such file or directory
> > Can't stat 1: No such file or directory
>
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
>
> > > sub process {
> > > #only files, not directories. skip . and .. files
> > > if (-f $_ and !/^\./){
> > > if (-M $_ >= $MINUTES){
> > > print "$File::Find::name\n";
>
> > perldoc -f -X
>
> > -M Script start time minus file modification time, in days.
>
> > Now, the main problem. You have
>
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
>
> > If you create a new file, -M on that file will return a negative number.
> > You have assigned to $MINUTES a positive number so the condition
>
> > -M >= $MINUTES
>
> > will never hold for any file created after the script has been started.
>
> > Because of this logic error, I am a bit confused about what you really
> > want. Assuming you mean what you say about files "older than one
> > minute", you need to compare to current time not to -M.
>
> > From perldoc perlvar:
>
> > $BASETIME
> > $^T The time at which the program began running, in seconds since
>
> > So, to find the files older than 60 seconds, you would use the
> > following:
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV/Src/Test" );
>
> > while ( 1 ) {
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
> > sleep 1;
>
> > }
>
> > sub process {
> > return if not -f or /^\./;
> > my $mtime = -M;
> > my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> > if ( time - $age > 60 ) {
> > print "$_ : $age\n";
> > }
>
> > }
>
> > __END__
>
> > --
> > (remove .invalid and reverse each component for email address)
>
> > comp.lang.perl.misc guidelines on the
WWW:http://www.rehabitation.com/clpmisc/


Posted by A. Sinan Unur on May 13, 2008, 6:17 pm
Please log in for more thread options

[ Do *not* top-post and do *not* quote messages in full ]

> I read the documentation for file::find but still a little unclear.

The module is File::Find. Case matters.

> Would this be the only thing I need to skip subdirectories and hidden
> files/directories?
> find( { wanted => \&process, no_chdir => 1 }, @directories );

Of course not. What gave you that impression.

> If so, then I don't need this line then right?.
> return if not -f or /^\./;

You do need that line in sub process to skip things that are not plain
files or files whose names begin with a dot.

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Similar ThreadsPosted
can't get out of infinite while loop August 17, 2007, 4:14 pm
infinite loop (newbie) March 8, 2005, 2:52 pm
MIME::Tools infinite loop June 20, 2005, 7:18 am
infinite loop when perl is trying to print warning November 22, 2007, 10:29 am
Getting all directories/files from current directory and using -d flag for the directories October 8, 2004, 2:05 pm
Extracting Directories and Sub Directories and Counting November 1, 2004, 9:48 pm
LWP::UserAgent infinite hang March 5, 2007, 6:18 pm
log monitor September 14, 2006, 10:04 am
How to monitor changes in a directory November 9, 2006, 5:13 pm
monitor for sleeping processes December 23, 2004, 10:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap