[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Edlug Archive Mar 2004
]
Re: [edlug] Perl - Only run n instances of script?
David R. Baird writes:
> Instead of writing a single lock file, you could write to 4. If
> all 4 exist, then lock them out.
>
> Or write the pids to a single file, and remove them when the
> script ends. Lock them out if there are 4 pids.
Aha, I see what you mean. Something along the lines of:
#!/usr/bin/perl -w
use Fcntl ':flock';
$LOCK1 = '/tmp/xxx.lock.1';
$LOCK2 = '/tmp/xxx.lock.2';
$LOCK3 = '/tmp/xxx.lock.3';
$LOCK4 = '/tmp/xxx.lock.4';
open LOCK1, ">$LOCK1" or die;
open LOCK2, ">$LOCK2" or die;
open LOCK3, ">$LOCK3" or die;
open LOCK4, ">$LOCK4" or die;
while( (flock LOCK1, LOCK_EX | LOCK_NB ||
flock LOCK2, LOCK_EX | LOCK_NB ||
flock LOCK3, LOCK_EX | LOCK_NB ||
flock LOCK4, LOCK_EX | LOCK_NB) == 0 )
{
print "Waiting for lock\n";
sleep 5;
}
print "Got lock\n";
sleep 60; # do work
print "Releasing lock\n";
close LOCK4;
close LOCK3;
close LOCK2;
close LOCK1;
exit;
Thanks, L.
-
----------------------------------------------------------------------
You can find the EdLUG mailing list FAQ list at:
http://www.edlug.org.uk/list_faq.html