1

I am working on a script for no good reason spawns two extra processes when it run. To combat this i wrote i file lock routine. But because these extra processes are running at the same time this does not seem to work at all. what am i missing here.

my $lockfile = '/tmp/lockfile';
my $fhpid;
sub BailOut {
    print "$0 is already running. Exiting.\n";
    print "(File '$lockfile' is locked).\n";
    exit(1);
}
open($fhpid, '>', $lockfile) or die "error: open '$lockfile': $!";
flock($fhpid, LOCK_EX|LOCK_NB) or BailOut();
Opad
  • 96
  • 1
  • 5
  • 4
    What do you mean by "Does not work"? Does it throw an error? Does it not detect the lock? Does it decide to play blackjack instead? – AKHolland Sep 04 '13 at 18:35
  • @AKHolland, As the OP said, the code allows multiple copies of the program to run at the same time even though it's suppose to prevent it. – ikegami Sep 04 '13 at 19:06
  • 2
    OS? File system? How do you know there are more than one copy of the program running? (Are you sure it's the processes simply haven't reached the test yet? Are you sure they're not zombies?) Does the handle go out of scope at any point in the execution of the program? – ikegami Sep 04 '13 at 19:07

0 Answers0