It seems the filehandle must be opened for writing, on AIX.  This works for me (tested on AIX 5.1, 5.3 and 6.1, with Perl 5.8.2 and 5.8.4):

use strict; use warnings; use Fcntl qw(LOCK_EX LOCK_NB); open DATA, ">>", $0 or die $!; flock DATA, LOCK_EX | LOCK_NB or die "already running\n"; print "started $$\n"; sleep 300; __DATA__

For example

$ ./761299.pl started 1220690 ^Z Suspended $ bg [1] ./761299.pl & $ ./761299.pl already running $ kill 1220690 [1] Terminated ./761299.pl $ ./761299.pl started 1220692

truss shows:

kfcntl(3, F_SETLK, 0x2FF22430) = 0 # when not already r +unning kfcntl(3, F_SETLK, 0x2FF22430) Err#13 EACCES # otherwise

(instead of __DATA__, you can of course also open SELF, or whatever)

Update: replaced open DATA, ">>", "/dev/null" with open DATA, ">>", $0  (with more than one program, you'd use the same file, otherwise)

A problem with this approach would be that the program couldn't be installed on/run from a read-only file system...


In reply to Re: Lightweight Solution To "Only 1 Process Running" On AIX (open for writing) by almut
in thread Lightweight Solution To "Only 1 Process Running" On AIX by Limbic~Region

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.