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.

In reply to singleton lock not reliable by pidloop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.