Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

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

A workmate asked me the best way to ensure that only one copy of his Perl script is running at a time. From a quick SuperSearch, a couple caught my eye:

I was intrigued by the adamk trick, so tested it with:

use strict; use warnings; use Fcntl qw(:flock); print "start of program\n"; unless (flock(DATA, LOCK_EX|LOCK_NB)) { print "$0 is already running. Exiting.\n"; exit(1); } print "sleeping 15...\n"; sleep(15); print "end of program\n"; __DATA__ This exists so flock() code above works. DO NOT REMOVE THIS DATA SECTION.
This works fine on Unix. On Windows, however, instead of exiting with a nice "already running" message, the perl script surprised me by printing nothing and exiting immediately with an exit code of zero (this is the same perplexing behaviour reported by BrowserUk in Re: Why no LockFile() in Win32API::File ?).

Stepping through the code in the debugger reveals the root cause of this odd behaviour. When the file is locked on Windows, you can open it ok but you get an error when you attempt to read from it. Now, the lower layers of the Perl IO system detect the read error all right (setting PerlIOBase(f)->flags |= PERLIO_F_ERROR), but the first PerlIO_getc() call in Perl_sv_gets() returns EOF ... so perl dutifully parses and runs an empty file, which explains why nothing happens and you get an exit code of zero.

So, being pressed for time, I suggested this variation on adamk's cute trick:

use strict; use warnings; use Fcntl qw(:flock); my $lockfile = 'mylockfile'; sub BailOut { print "$0 is already running. Exiting.\n"; print "(File '$lockfile' is locked).\n"; exit(1); } print "start of program\n"; open(my $fhpid, '>', $lockfile) or die "error: open '$lockfile': $!"; flock($fhpid, LOCK_EX|LOCK_NB) or BailOut(); print "sleeping 15...\n"; sleep(15); print "end of program\n"; # Note: lock should be automatically released at end of program, # no matter how the process is terminated.
Though this simple solution seems sound to me, it is easy to overlook subtle problems and race conditions. So if anyone can see a flaw in this solution or has any other advice, please respond away.


In reply to Ensuring only one copy of a perl script is running at a time by eyepopslikeamosquito

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 avoiding work at the Monastery: (7)
    As of 2014-07-11 01:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (217 votes), past polls