Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Easiest way to protect process from duplication.

by JavaFan (Canon)
on Jan 20, 2012 at 20:23 UTC ( #949042=note: print w/replies, xml ) Need Help??

in reply to [Solved]Easiest way to protect process from duplication.

use Fcntl ':flock'; open my $fh, "+<", $0 or exit; exit unless flock $0, LOCK_EX | LOCK_NB;
Note that depending on your OS and how you call the program, a second open may fail (and then, the flock isn't necessary).

Replies are listed 'Best First'.
Re^2: Easiest way to protect process from duplication.
by gnosti (Friar) on Jan 23, 2012 at 05:52 UTC
    Here's a variation that uses the __DATA__ handle.
    use Fcntl qw(LOCK_EX LOCK_NB); die "Another instance is already running" unless flock DATA, LOCK_EX|L +OCK_NB;
      That actually requires a __DATA__ (or __END__) token to be present.
        Thank you too. BTW __END__,__DATA__,etc is there any general name for these tokens, (like "Labeled blocks", "builtins",...)? I'm newbie in perl so I just wanted to read about this stuff.
      Thank you for your post, that is what I really need.
Re^2: Easiest way to protect process from duplication.
by kazak (Beadle) on Jan 21, 2012 at 06:16 UTC
    Thanks for your attention, everyone. Program supposed to run in a background. (I mean # &) so is it changing something ?
      No, why should it?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949042]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2016-10-24 09:36 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (304 votes). Check out past polls.