Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Check for another running instance of the same program

by vinoth.ree (Monsignor)
on May 14, 2013 at 03:18 UTC ( #1033391=note: print w/replies, xml ) Need Help??


in reply to Check for another running instance of the same program

LanX ++

There are many ways to ensure single instance of perl program is running. PID files are the traditional way to do it. we can also hold a lock on a file,for example the program itself. This small piece of code will do the trick:

use Fcntl ':flock'; open my $self, '<', $0 or die "Couldn't open self: $!"; flock $self, LOCK_EX | LOCK_NB or croak "This script is already runnin +g";

One advantage over PID files is that files automatically get unlocked when the program exits. It is much easier to implement in a reliable way.


All is well

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033391]
help
Chatterbox?
[MidLifeXis]: :-)
[MidLifeXis]: Not for a few years yet. Kids in school :-)
[LanX]: NQC
[MidLifeXis]: Is there internet access up there on the lakes? If I moved there I would like to be able to fish :-D
[LanX]: Not Quite Canada
[Discipulus]: have you been fired?!?
[LanX]: remote rules, I've been offered jobs in the US with the promise to stay outside
[MidLifeXis]: Nope. Just looking around, as there are multiple culture and business changes in the area I am in.
[MidLifeXis]: not yet, anyway. Who knows. Just do my best and whatever happens happens.
[LanX]: should be even easier for you

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2017-03-23 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (286 votes). Check out past polls.