Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 (Hermit) 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]
[marto]: I'm all for that
[choroba]: Trainspotting without OV is just a half of the movie!
LanX just single handedly stopped Brexit!

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-02-19 10:51 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (262 votes). Check out past polls.