Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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).


Comment on Re: Easiest way to protect process from duplication.
Download Code
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 # test.pl &) so is it changing something ?
      No, why should it?
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-21 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls