At most 1 running copy

Ed Wilts ewilts at
Sat Nov 27 13:59:23 UTC 2004

On Sat, Nov 27, 2004 at 08:06:40AM -0500, Paul Tomblin wrote:
> On Fri, 26 Nov 2004 22:49:51 -0600, Ed Wilts <ewilts at> wrote:
> > use FileHandle;
> > use Fcntl qw(:DEFAULT :flock);
> > my $FH = new FileHandle;
> > sysopen(FH, "ftphandler.lock", O_RDWR | O_CREAT) or die "can't open ftphandler.lock: $!";
> > flock(FH, LOCK_EX | LOCK_NB) or exit;
> > print "running...\n";
> > [rest of code here]
> > close (FH);
> > unlink ("ftphandler.lock");
> Two things I see off the top of my head:
> - O_RDWR|O_CREAT won't fail if the file already exists.  How does this
> stop people from running it twice if the secon one starts up before
> the first one has time to flock it?  Wouldn't you be better off using

The condition I need to test for is the possibility of a cron job
starting a second copy before the first completes.  Since the
granularity of cron jobs is to the minute, this isn't a problem for me.
The users that are running these cron jobs don't have interactive
access to run the script.

> - Beware of using flock (or O_CREAT) on NFS mounted file systems. 

Yup, that's in the Perl FAQ too.  Not an issue for me.

Ed Wilts, RHCE
Mounds View, MN, USA
mailto:ewilts at
Member #1, Red Hat Community Ambassador Program

More information about the users mailing list