Reputation: 3262
I have 5 perl scripts and want to insure that if someone run one of the scripts and try on the same time to run another script the second script will die with message that another script is running
I see this mechanism on other stackoverflow deal with one instance: What's the best way to make sure only one instance of a Perl program is running?
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 running";
my question is there are an option to build some mechanism to make sure only one script run on windows without the need to lock an external file on all the scripts ?
Upvotes: 1
Views: 471
Reputation: 11677
If you have five separate scripts and none of them can be run simultaneously, you need some way of accessing a shared data source between them. That shared source tells whether or not one of them is running and that can be awfully tricky to manage correctly. If you don't like the lock mechanism, you could try a database, Redis, or something similar, but I don't particularly see the value of that. If you feel that using flock
is clumsy, then extract it into a module and just use that module or find some other way of managing your shared information, such as a database "SELECT FOR UPDATE" in lock mode statement.
My question: if you don't want to lock an external file, what is the reason behind this? If we knew that, we may be able to do a better job of answering your question.
Upvotes: 1