Re: perl daemon
by arhuman (Vicar) on Feb 08, 2001 at 14:54 UTC
|
Hey !
Go on ! It shouldn't be too difficult to write an infinite loop with a sleep,a test on the existence of a file and fire it up in the background
We all want to help.
But you have to do SOMETHING...
Hope I'm not too rude, and still ready to answer if you have any particular problem.
| [reply] |
Re: perl daemon
by t0mas (Priest) on Feb 08, 2001 at 15:28 UTC
|
If you want your program to be a "real" daemon, you will
probably want to use Proc::Daemon to daemonize your process
before you do the checking for the file.
/brother t0mas
| [reply] |
Re: perl daemon
by MeowChow (Vicar) on Feb 08, 2001 at 14:56 UTC
|
Here's a one-liner for you:
perl -e '-e $ARGV[0] ? sleep 10 : die "GONE!" while 1' <FILENAME>
Of course, the complexity of your final script depends entirely on what you want to do when the file disappears, which you haven't quite stated...
MeowChow
print $/='"',(`$^X\144oc $^X\146aq1`)[-2] | [reply] [d/l] |
Re: perl daemon
by mr.nick (Chaplain) on Feb 08, 2001 at 19:03 UTC
|
Here is a minor example of something that would work.
#!/usr/bin/perl
use strict;
## become a daemon
exit if fork();
## loop and look for a filename appearing
my $fn="/homes/bob/important_file.txt";
while (1) {
if (-f $fn) {
## a file was found, do something
DoSomething();
## then exit
exit;
}
## pause 5 minutes before checking again
sleep 300;
}
## never here!
exit;
| [reply] [d/l] |
|
|
while (1) {
if (-f $fn) {
## a file was found, do something
DoSomething();
## then exit
exit;
}
## pause 5 minutes before checking again
sleep 300;
}
really be:
while (1) {
if (-f $fn) {
## a file was found, do something
DoSomething();
## then break the loop
break; ## <<-- not exit();
}
## pause 5 minutes before checking again
sleep 300;
}
| [reply] [d/l] [select] |
|
|
exit is fine here: it exits the program. There is no 'break' in Perl, but there is a last, which does the same thing. I would have used 'last' myself as well, but 'exit' works fine.
| [reply] |
Re: perl daemon
by doran (Deacon) on Feb 09, 2001 at 00:09 UTC
|
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...
| [reply] [d/l] |
Re: perl daemon
by AgentM (Curate) on Feb 08, 2001 at 20:54 UTC
|
Polling is a solution, but certainly not the best. If you're looking for security stuffs, check out TripWire. It will even notify you of break-ins and who did it. Nuke the guesswork and polling and install tripwire.
AgentM Systems nor Nasca Enterprises nor
Bone::Easy nor Macperl is responsible for the
comments made by
AgentM. Remember, you can build any logical system with NOR.
| [reply] |
Re: perl daemon
by $code or die (Deacon) on Feb 08, 2001 at 19:19 UTC
|
| [reply] |
Re: perl daemon
by arturo (Vicar) on Feb 08, 2001 at 19:18 UTC
|
Another option: write a shell script (batch file, whatever makes sense on your system) and have your system's scheduler (e.g. cron on *nix systems) check for the presence of the file every so often (whatever interval makes sense).
Philosophy can be made out of anything. Or less -- Jerry A. Fodor
| [reply] [d/l] |