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

If all that you need to know is if another script is running, why not instead of using flock, just create an empty file and check if it exists.

Script 1:
my $file = 'c:\temp\running.tmp'; open(RUNNING, ">$file"); close(RUNNING); ##Do your stuff unlink($file);

Script 2:
my $file = 'c:\temp\running.tmp';' if(-e $file){ print "Running\n"; }else{ print "Not Running\n"; }

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:30 UTC
    That won't work... two processes can both look to notice a file doesn't exist, then both "create" it.

    To properly lock, you must have an atomic "test-and-set" operation that is uninterruptable by another process.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I realize that there is a mutual exclusion issue. Looking at the original post, he asked how to tell if the other process was running. Perhaps a better question would have been, how can I check if another process is in a critical section of the code. Normally with threads, you can use a semaphore. I guess I didn't look enough into the question, I just answered the one that was posed.
      Thanks for the caveat.
Re: Re: use flock() to determine if other script is running
by Red_Dragon (Beadle) on Aug 21, 2003 at 15:31 UTC
    Thank you very much for your input.