Here's what I use. It checks a defined directory every n seconds. It also checks the keyboard every second and exits if a key has been pressed. This script acts on anything found in the target directory. Modify the regex in check_directory() to change this.
#!/usr/bin/perl -Tw use strict; use Term::ReadKey; # Directory to check my $data_dir='/path/to/directory'; # Seconds between directory checks my $poll_wait='5'; TEST:{ # Process any incoming files if (check_directory()){ process_files(); redo TEST; } else{ # Otherwise check for any key press while waiting my $counter=0; ReadMode('cbreak'); CHECK:{ my ($char); # Check if the user pressed a key if (defined ($char = ReadKey(-1))){ ReadMode('normal'); exit(); } # Is it time to check the incoming directory? elsif($counter >= $poll_wait){ $counter=0; ReadMode('normal'); redo TEST; } # Sleep a bit before checking the kyb again. else{ sleep(1); $counter++; redo CHECK; } } } } # We should never get here, but this restarts the loop if we do redo TEST; sub check_directory{ print "\nChecking directory...\n"; opendir DIR, "$data_dir" or die "Problems opening $data_dir: $!"; while ( my $dir = readdir DIR ) { next if ( $dir =~ m/^\.\.?$/ ); closedir DIR; return 1; } closedir DIR; print "Directory Empty\nPress A Key to Stop\n"; return 0; } sub process_files{ # do somthing like... print "The directory has something!\n"; exit(); }

good luck...

In reply to Re: perl daemon by doran
in thread perl daemon by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.