Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Using Trigger Files

by azatoth (Curate)
on Jun 14, 2001 at 15:49 UTC ( [id://88370]=perlquestion: print w/replies, xml ) Need Help??

azatoth has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I am currently in the process of writing a tiny script to ftp files over to an NT box from one of our unix servers.
#!/usr/bin/perl -w use strict; use Net::FTP; use Getopt::Long; my %opt = (); GetOptions( \%opt, "usr=s", "pwd=s", ); chdir("/home/export/data/"); @files = reverse ( map { split (/\s+/))[8]; } `ls -lrt Open_ +Trade_Summary*`); foreach my $file (@files[0..3]) { check_age($files); } sub check_age{ my $file = shift; ( (if -M $file) < .5 ) { print "$file is current - ok to send\n"; send_file($file); } else { print "No current data available\n"; } sub send_file{ my $file = shift; my $ftp = Net::FTP->new("ntbox01.host.org"); $ftp->login($opt{usr}, $opt{pwd}); $ftp->cwd($remDir); $ftp->put($file); $ftp->exit; }
This is fine. I can cron this and have it check for files whenever. But what I would really like to do is put some sort of loop in the script, that checks say, every 10 minutes, for new files. I can tell it what exact file to look for, but need some kind of "Trigger" functionality to kick off the script...Anyone have any ideas / follow me at all?

Thanks as always,

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?

Replies are listed 'Best First'.
Re: Using Trigger Files
by tadman (Prior) on Jun 14, 2001 at 16:24 UTC
    An easy way is to put this in a function, and call that function from a loop every so often. Consider:
    my $time_to_run = time; while ($time_to_run) { # If it's time to run... if (time < $time_to_run) { DoSomething(); # ...then wait 10 minutes (60 seconds * 10) $time_to_run += 60*10; } else { # Snooze for 10 seconds select(undef,undef,undef,10.0); } }
    You might also want to put in %SIG signal handlers to let it gracefully exit from the FTP session when sent an INTR signal, or HUP perhaps. This way you can give it the boot without corrupting any uploads. After all, this program is in an infinite loop, but on purpose.
Re: Using Trigger Files
by Zaxo (Archbishop) on Jun 14, 2001 at 16:47 UTC

    How about:

    TRADES: { my @files = list_readies(); # sub is like example send_files( @files); # like send_file, but acts on list of files sleep(60*$minutes); redo; }

    ...running nohup in background. You might want to redirect STDIN, STDOUT to /dev/null, STDERR to a logger process so it's like a user daemon

    I'd be inclined to stick with the cron job idea.

    After Compline,
    Zaxo

Re: Using Trigger Files
by wardk (Deacon) on Jun 14, 2001 at 17:40 UTC

    Since you can use cron, how about just telling cron to execute the script every 10 minutes? this would prevent you from having to keep tabs on it if for whatever reason it hangs/dies. I don't know what sort of control you have with the server, but if it's ever down for maintenance, you are going to have to restart your script. cron will come up with the box and every 10 minutes your job will run. If anything you gain the peace of mind knowing that your script even if it dies a horrible death, with just start back up again without your intervention. (covers you on weekends, vacations, pub crawls... :-)

    is the requirement to execute every 10 minutes, or is it to act on lets say a new file appearing on the system (about every 10 minutes)? if you need to act immediately then I suppose cron may not be the best tool, then again you could alway increase the frequency of the cron job.

    Seems a good candidate for cron, plus you don't have to code a quasi-scheduler yourself (leverage others hard work).

    good luck with whatever you decide!

      Bah! Can't edit Parent Node.

      This is what I meant by using the cron. I could easily put in a line to run my script every ten minutes (which would check for files every 10 minutes), but I was just wondering if there was an easy way to do "triggers".

      Although the more I think about it, the more sense it makes to run it as a cronjob, mainly due to your point about dying / restarting.

      Just a whimsical query more than anything...Thanks for the useful replies & insights as always...

      Azatoth a.k.a Captain Whiplash

      Make Your Die Messages Full of Wisdom!
      Get YOUR PerlMonks Stagename here!
      Want to speak like a Londoner?
Re: Using Trigger Files
by arturo (Vicar) on Jun 14, 2001 at 17:02 UTC

    How about creating a text file in a specified location that holds the name(s) of files that are added; the master control loop checks for the existence of this file, reads it in, then deletes it.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Using Trigger Files
by mattr (Curate) on Jun 14, 2001 at 18:02 UTC
    What would you want the program to do besides waiting for the appointed time? If the answer is nothing, I'd recommend you just use cron and keep it simple. As mentioned above, you don't have to worry about checking that your system is functional, whether it is your eyes on a window periodically, or some other program that pings your system, and *that* would probably run from cron.

    But you could also use cron to run a perl program every minute or so which just checks a few things and decides whether it really wants to trigger your main program. That way you could do most of your scheduling in Perl.

    You should also remember that if you restart your computer, you will want to have a boot script to start your uncronned program every time you restart as well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://88370]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found