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

Check for another running instance of the same program

by Narveson (Chaplain)
on May 13, 2013 at 21:41 UTC ( [id://1033362]=perlquestion: print w/replies, xml ) Need Help??

Narveson has asked for the wisdom of the Perl Monks concerning the following question:

We have a Perl utility (let's call it utility.pl) that is supposed to avoid starting if another instance of itself is already running. The way we've implemented this requirement is to check for the existence of a semaphore file, semaphore.txt, in a particular directory. If the semaphore file exists, the utility program dies with a message saying
Either another instance of utility.pl is already running, or the file semaphore.txt needs to be deleted.
If the semaphore file doesn't exist, the utility first writes it, then does its work (which can take minutes), then unlinks it. Is this the best way to do this, or can somebody suggest a better way?

Update

Thank you, LanX, for the link to the thread from 2006. What I meant by "better" was "not theoretically constituting a race condition". Also not leaving its semaphore file lying around when it crashes, which has happened. The solution that opens and locks $0 is my favorite so far, but I'm on Windows, so the second running instance dies silently without divulging my error.

Replies are listed 'Best First'.
Re: Check for another running instance of the same program
by LanX (Saint) on May 13, 2013 at 22:15 UTC
Re: Check for another running instance of the same program
by dd-b (Monk) on May 13, 2013 at 22:34 UTC

    You can safely automate the deletion of the semaphore file by writing the PID to it, and having later programs check if the PID exists as a running process; if not it can delete the file and proceed.

    Consider your requirements carefully; for example, does it apply to one system, or all systems that can access a particular NFS mount, or all systems on a built-in list with IP connectivity, or what exactly. These lead to very different solutions. Do you want to consider expanding it to "no more than n" copies running at once, or is the simple one at a time good?

Re: Check for another running instance of the same program
by vinoth.ree (Monsignor) on May 14, 2013 at 03:18 UTC
    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
Re: Check for another running instance of the same program
by ww (Archbishop) on May 13, 2013 at 22:16 UTC
    It's one plausible way... among many. Most of them have occasioned some discussion here. Super_Search and search engines are your friends.

    Advising you that x or y or z is the "best" way without a lot more details on your case would be "iffy," at best.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: Check for another running instance of the same program
by Anonymous Monk on May 14, 2013 at 05:01 UTC
Re: Check for another running instance of the same program
by scorpio17 (Canon) on May 14, 2013 at 14:03 UTC
Re: Check for another running instance of the same program
by sundialsvc4 (Abbot) on May 14, 2013 at 02:25 UTC

    /me nods ...

    Semaphore-files are easy and quick, especially with regard to an activity that “takes minutes to perform.”   Is there a particular reason why you might be second-guessing this decision now?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1033362]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found