infinite loop to monitor directories

Discussion in 'Perl Misc' started by mr.vlad.dracula@gmail.com, May 13, 2008.

  1. Guest

    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;

    }
    , May 13, 2008
    #1
    1. Advertising

  2. wrote in news:5ff50268-c5d2-41d3-93dd-
    :

    > 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{HOME}/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__


    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, May 13, 2008
    #2
    1. Advertising

  3. dracula Guest

    Thank you very much. I appreciate your inputs and suggestions.

    /dracula


    On May 13, 10:50 am, "A. Sinan Unur" <> wrote:
    > wrote in news:5ff50268-c5d2-41d3-93dd-
    > :
    >
    > > 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{HOME}/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__
    >
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/
    dracula, May 13, 2008
    #3
  4. dracula Guest

    Re: infinite loop to monitor directories -one last question

    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{HOME}/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>




    On May 13, 11:18 am, dracula <> wrote:
    > Thank you very much. I appreciate your inputs and suggestions.
    >
    > /dracula
    >
    > On May 13, 10:50 am, "A. Sinan Unur" <> wrote:
    >
    > > wrote in news:5ff50268-c5d2-41d3-93dd-
    > > :

    >
    > > > 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{HOME}/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__

    >
    > > --
    > > A. Sinan Unur <>
    > > (remove .invalid and reverse each component for email address)

    >
    > > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/
    dracula, May 13, 2008
    #4
  5. Re: infinite loop to monitor directories -one last question

    dracula <> wrote in
    news::

    [ 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

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, May 13, 2008
    #5
  6. Ben Morrow Guest

    Quoth "A. Sinan Unur" <>:
    >
    > 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:
    >

    <snip>
    > my $mtime = -M;
    > my $age = $^T - $mtime * SECONDS_PER_DAY ;
    >
    > if ( time - $age > 60 ) {


    ....or forget both -M and $^T (especially since converting a time to a
    float and back probably isn't a good idea) and just use

    use File::stat;

    if (time - stat($_)->mtime <= 60)

    Ben

    --
    I must not fear. Fear is the mind-killer. I will face my fear and
    I will let it pass through me. When the fear is gone there will be
    nothing. Only I will remain.
    Frank Herbert, 'Dune'
    Ben Morrow, May 14, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    482
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    511
    Jeffry van de Vuurst
    Jul 30, 2003
  3. Replies:
    5
    Views:
    586
    benben
    Jan 31, 2006
  4. ScottZ
    Replies:
    2
    Views:
    349
    Jorgen Grahn
    Nov 2, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page