Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

At that point, I decided to actually store the job's process id in the lock file.

The OS is better at controlling concurrency because it can do clean up for dead processes. Whether the file is locked or not could be used to indicate concurrency.

package Cron::AvoidMultipleRuns; use strict; use warnings; use Cwd qw( realpath ); use Errno qw( EWOULDBLOCK ); use Fcntl qw( LOCK_EX LOCK_NB ); use File::Spec::Functions qw( rel2abs ); my $lock_file; my $lock_fh; { $lock_file = realpath(rel2abs($0)) . '.lock'; open($lock_fh, '+>>', $lock_file) or die("Can't create lock file \"$lock_file\": $!\n"); if (!flock($lock_fh, LOCK_EX|LOCK_NB)) { undef $lock_fh; if ($! == ($^O =~ /Win32/ ? 33 : EWOULDBLOCK)) { die("Another instance of this program is running. Exiting. +\n"); } else { die("Cannot lock lock file \"$lock_file\": $!\n"); } } } END { if (defined($lock_fh)) { undef $lock_fh; # Release lock unlink($lock_file) ;#or warn("Can't unlink lock file \"$lock_file\": $!\n"); } } 1;

You had a bug where the lock can be defeated (accidentally or otherwise) by using a symlink to the script. It's fixed by realpath(rel2abs()) in my code.

die is overkill (punny!) for errors in removing the pid file. In fact, even a warning is sounds unnecessary to me.

An even better method for Windows would be to create a named mutex instead of creating a file. That way, it gets cleaned up automatically.

Alternatively, You could simplify the code a lot by simply locking the script.

package Cron::AvoidMultipleRuns; use strict; use warnings; use Errno qw( EWOULDBLOCK ); use Fcntl qw( LOCK_EX LOCK_NB ); our $lock_fh; { open($lock_fh, '<', $0) or die("Can't open script \"$0\": $!\n"); if (!flock($lock_fh, LOCK_EX|LOCK_NB)) { undef $lock_fh; if ($! == ($^O =~ /Win32/ ? 33 : EWOULDBLOCK)) { die("Another instance of this program is running. Exiting. +\n"); } else { die("Cannot lock script \"$0\": $!\n"); } } } 1;

I really dislike the name of the module. The module has nothing to do with cron and is not just useful for cron scripts, for starters. Then there's the problem that you don't want to prevent multiple runs. You want to prevent simultaneous runs.

A useful improvements would be to allow the caller to specify a lock file name if he's not happy with the default.


In reply to Re: RFC: A new module to help avoid running multiple instances of the same script (via cron, for example) by ikegami
in thread RFC: A new module to help avoid running multiple instances of the same script (via cron, for example) by scorpio17

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2019-11-12 21:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (66 votes). Check out past polls.

      Notices?