SPUG: Perl Question?

DeRykus, Charles E charles.e.derykus at boeing.com
Wed May 11 13:19:41 PDT 2005


> you might instead consider one of my favorite locking hacks:

>  #!/usr/bin/perl
>  ...
>  ## ensure only one instance of this script is running
>  INIT { flock DATA => LOCK_EX | LOCK_NB or exit 1 }
>  ...<script body here>...
>  __DATA__

> which will exit if the script can't get an exclusive lock on itself. no messy lockfiles to worry about. ~jerry


Intriguing suggestion but won't this fail on most systems since the DATA
filehandle is readonly.... ?


See thread below:
http://groups-beta.google.com/group/comp.lang.perl.misc/browse_thread/thread/d05e19ca6f8d5e19/8da7e94a9a233bb4?q=flock+__DATA__+group:*perl*&rnum=23&hl=en#8da7e94a9a233bb4

Here's the relevant exchange:

     John Lin> use Fcntl ':flock'; 
     John Lin>  $| = 1; 
     John Lin> flock(DATA,LOCK_EX|LOCK_NB) or die "$0 is executing\n"; 
     John Lin> print sleep 1 for 1..10; 
     John Lin> __DATA__ 

     John Lin> Great!!!  When one program is running, the same program cannot be 
     John Lin> evoked again.  But no die messages are shown because perl is unable 
     John Lin> to open the script at all. 
     ...

     Anno Siegel> The problem is again that some systems don't give you an exclusive lock 
     Anno Siegel> for a file that is only open for reading. 


Fails on my older Solaris OS for instance.

--
Charles DeRykus 


More information about the spug-list mailing list