while (1) { # Are we supposed to pause or stop? if (-E "/tmp/pause") { sleep 10; next; } last if -E "/tmp/STOP"; # Find a file to process my @filist = glob("*.input"); while (my $file = shift @filist) { # try to claim the file if (rename $file, "$file.working.$$") { # Successful, so go handle it process_file("$file.working.$$"); last; } # Didn't get the file (someone else might've claimed it just # as we tried to), so just loop to the next one... } }