how to prevent my perl script in running multiple instance?

my script gets call multiple times within the same second.

since the script will be doing file i/o and verification, i don't want more than one instance running at any given time.

what are the code that i can use quit one instance if another one is already running?

i've tried creating a lock file and seems doesn't work since multiple instance taking places almost simultaneously.

what are the possible solutions?
Who is Participating?
Adam314Connect With a Mentor Commented:
When program terminates, normally or not, lock is removed.

use strict;
use warnings;
use Fcntl qw(:flock);

my $LockFile = 'lockfile';

open(my $in, '>>', $LockFile) or die "Could not open lockfile: $!\n";
if(!flock($in, LOCK_EX | LOCK_NB)) {
	warn "Could not get lock - program must be already running.\n";
#do whatever else here

Open in new window

Create a lock file, and lock (using the flock function) it non-blocking.  If the flock fails, then another instance must be running, so exit.
jay28leeAuthor Commented:
could you show me the exactly implementation coding?

how do i verify if the flock fails?

do i remove the lock file at the end of execution?

what if there's an abnormal termination of the process?  what to do with the lock file in this exception?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

jay28leeAuthor Commented:
i'll give it a try later tonight when i have a chance.

the if statement is testing if the program is already running.

#do whatever else here is when testing passed.

what does the "open" statement do exactly?  under what condition is "could not open lockfile"?  disk full?  isn't it the program will always wait until it can complete the open statement?

The open statement opens the specified file ($LockFile) in append mode, and associates it with the specified handle ($in).  If the open fails, the program dies.  Under "normal" conditions, this will never fail.  Things that could cause it to fail:
    * disk is full
    * No write permission to $LockFile
    * Other non-likely things.
It will open the file even if another instance of your program has it open.

The flock statement will try to lock the file.  Only 1 lock for a file is allowed at a time, so if another instance is running, this will fail.  The default behavior of flock is to wait until the file becomes available.  With the LOCK_NB option, the function will return immediately - true if successful, false otherwise.  So, the "if" statement is actually checking to see if it can lock the file.  Since this is the first thing the program does, if another instance is running, it'll have the file locked, and this instance will fail, and exit.
jay28leeAuthor Commented:
how does abnormal termination of the program being handled?

does the lock file always exist in the system?  only lock flag being switched on and off?
jay28leeAuthor Commented:
this piece of code is working fine.

just one question, the close($in) is only taking place within the if statement, outside of that, the file is never been closed, is that going to cause any issue?
Perl will automatically close the file when the program ends.  If you close it before that, it'll remove the lock.

If the file does not exist, it will be created by the program.  After that, the file will always exist, unless you delete it.  The program does not remove the file.
jay28leeAuthor Commented:
Thanks for the clarification.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.