http://qs321.pair.com?node_id=11134206

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

Oh great Monks, I am having an issue ensuring only one instance of my script runs at a time. Any help appreciated. This is a snippet version showing my technique:
use strict; use warnings; use Fcntl ':flock'; # only one of us open my $ME, '<', $0 or die "Couldn't open self: $!"; flock $ME, LOCK_EX | LOCK_NB or exit; sleep;
This program is run every minute from a crontab. Doing a ps I expect to see exactly one instance. However, after a random period ranging from minutes to months I find two instances are running. I don't think it's an OS issue because I am seeing this on three different systems: centos 6 linux, macOS 11.4 and FreeBSD 12.2-RELEASE. So I'm actually hoping there is something wrong with this technique so I can fix it or there is a better way. Thanks for your time and wisdom.