Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

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

Thanks for the feedback, everyone.

Here's my revised version:

package HighLander; use strict; use Fcntl qw( LOCK_EX LOCK_NB ); open(our $fh, '<', $0) or die("Can't open \"$0\": $!\n"); unless ( flock($fh, LOCK_EX|LOCK_NB )) { print "Another instance of \"$0\" is already running. Exiting...\n"; exit(0); } 1;

Here's an example of something using it:

#!/usr/bin/perl use strict; use HighLander; # if this script is already running, you'll never make it this far... print "Running...\n"; sleep 10;

A few notes:
I like using flock much better than creating a pid file. But creating a lock file is just as annoying. Getting a lock on the script itself is brilliant! When the process goes away, so does the lock, so the issue with bogus pid files lying around after interrupted jobs goes away, too. I've tested this on RedHat linux (RHEL5). I've also tried it using a run of the real script and a run with a symbolic link - and it does the right thing in that case also. On WinXP, using perl v5.8.7 (ActiveState build 813), it seems to work - sort of - the second job exits, but I don't see the "Another instance is already running" message. But the original intent was to use this with cron, which doesn't apply to WinXP anyway. Win32::Mutex is probably more appropriate for Windows users. And if you really need the process id, Proc::Pidfile seems like a better choice.

ikegami: I don't understand this line from your code:

if ($! == ($^O =~ /Win32/ ? 33 : EWOULDBLOCK)) {

This tests for an error condition, after getting a lock, and if you're running on Windows... 33?! It seems to work okay without this check, but I'd like to understand this better, for future reference.

Also - it seems like the file handle ($fh) needs to be a global variable, so I used 'our' instead of 'my'. If I use 'my', then when the package goes out of scope, the lock gets dropped!

mortiz: I'd like to add something like this:

use Highlander qw( :verbose );
To turn on the print statement. Something like this:
... unless ( flock($fh, LOCK_EX|LOCK_NB )) { if ($verbose) { print "Another instance of \"$0\" is already running. Exiting...\n +"; } exit(0); } ...

... but I don't see how to accomplish this using only an export tag. Any hints?

I'm not planning to upload this to CPAN. It seems too simple to bother. Besides, merlyn solved this problem over 9 years ago! He should get the credit, not me.

Thanks again for the helpful comments, everyone.

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

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2019-11-18 17:16 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (90 votes). Check out past polls.