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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.