in reply to use flock() to determine if other script is running

The semantics for nonblocking flock are to return false if the lock was not obtained, true if it was. If the platform does not support flock a fatal error is thrown.

You are apparantly on a win32ish system, which may or may not support flock, so it would be good to catch the error. See perlport and perlwin32 for details. Try something like this,

use Fcntl ':flock' ; open ONE, ">> C:\\temp\\RTEOlock.log" or die $!; my $lck = eval {flock ONE, LOCK_EX | LOCK_NB}; die 'flock not supported! ', $@ if $@; if ($lck) { # The other script is not running code here } else { # The other script is running code here }
For this semaphore system to be effective, the other script should flock the flag file first thing, and drop the lock at the very last, perhaps in a DESTROY() sub.

After Compline,
Zaxo

Replies are listed 'Best First'.
•Re: Re: use flock() to determine if other script is running
by merlyn (Sage) on Aug 21, 2003 at 00:29 UTC

      Perhaps for VMS and suchlike, I don't know. On AS build 630 (perl 5.6.1)/Windows95, which does not support flock, my code gives:

      C:\download>perl -c flock.pl flock.pl syntax OK C:\download>perl flock.pl flock not supported! flock() unimplemented at flock.pl line
      at which point winders wrote off the edge of the screen and lost the line number ;-)

      My 'fatal error' jargon came directly from 'perldoc -f flock'.

      After Compline,
      Zaxo

Re: Re: use flock() to determine if other script is running
by Red_Dragon (Beadle) on Aug 21, 2003 at 15:22 UTC
    Thank you, Perl Monks, for providing the venue where such novice questions can be aired and Zaxo, thank you for the code snippet. As someone that is struggling to learn Perl and has no mentor to turn to I do appreciate your investment in me.

    Here is a brief description of what I am trying to do. Script ONE is activated by a message console and passed some message text which it turns into arguments against two SQL tables taking the extracted data and placing it in a data repository table then checks to see if script TWO is running. if no, starts it then in either case, script ONE ends.

    Script TWO checks the repository table for an elligible candidate, when found writes it to Remedy then ends. All this to be done by a guy who as yet does not have a firm grasp of Perl basics hence my great appreciation for this site(it gives me hope).