cerr wrote:
Hi There,

I use below code to make sure i have only one instance of my script
running at a time. But weirdly enough i sometimes seem to have running
two instances. This script is getting called on a regular basis by a
cron job and may take a long time (30+min)to complete but still...I
can't figure out what would be wrong with this code, anyone?

On a cron started program you might choose to do things differently if the execution time takes a significant amount of the duty cycle.

I usually do it like this: Start a single program (once on system boot) that is it's own scheduler. This scheduler starts the actual worker, runs it and checks how long it ran. If it's longer then the configured "duty cycle" (for you: once per hour), send an error mail. If it ran shorter, sleep() out the remaining time.


Something like this:
#!/usr/bin/perl -w

use strict;
use warnings;
 my $cycleStartTime;
my $cycleTime = 3600; # 1 hour

# .. do some initialization

$cycleStartTime = time;
while(1) {
    # your worker code goes here
    # doSomething();

    # do some time calculation
    my $tmptime = time;
    my $workTime = $tmptime - $cycleStartTime;
    my $sleeptime = $cycleTime - $workTime;

    if($workTime > $cycleTime) {
       # Here you notify the user
    } elsif($sleeptime > 0) {
        print "** Fast cycle ($workTime sec), sleeping for $sleeptime sec **\n";
        sleep($sleeptime);
        print "** Now back to work **\n";
    } else {
        print "** Cycle time matched exactly **\n";
    }
    $cycleStartTime = time;
}


LG
Rene

--
#!/usr/bin/perl #99BoB (C)2004 cavac:prg count drink vessel place act1 act2
@a...@argv;$c=$a[0]||99;$b=" ".($a[2]||"bottles")." of ".($a[1]||"beer");$w=" ".
($a[3]||"on the wall").",";do{print"$c$b$w\n$c$b,\n".($a[4]||"take one down").
", ".($a[5]||"pass it around").",\n".--$c."$b$w\n\n";}while($c);print"END!\n";

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to