Don't know if using a flag(better: lock)-file is the best way to do it, but it should work for your purpose. Something along...

BEGIN { our $LOCKFILE = "/tmp/frogs.pid"; die "Another process is already running (see content of $LOCKFILE for + pid)...\n" if -e $LOCKFILE; open my $out, '>', $LOCKFILE or die "cannot open $LOCKFILE - $!"; print $out "$$\n"; close $out; # chmod 0600 ... if you like } warn "running... with PID: $$ ...\n"; sleep 10; END { unlink $LOCKFILE or die "cannot remove lock-file $LOCKFILE - $!"; }
It is not race-condition proof, but 10-20 minutes in between runs is not a race condition, I guess.
Update: Ok, when using cronjobs, there is a probability not equal to zero that a cronjob may collide one day... interacting with a manual invocation.
Update2: Used /tmp instead of /var/run since you might not have the privileges (root) to access files there. If you do, /var/run is closer to best practises. You might also need to register SIGnal handlers to remove the lock file.


In reply to Re: Abort if instance already running? by Perlbotics
in thread Abort if instance already running? by pileofrogs

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.