Highlander!

There can be only one! Referring to the one process doing a particular job at a time, of course.

use 5.14.2;
use warnings;
use IPC::ConcurrencyLimit;

run();
exit(0);

sub run {
my $limit = IPC::ConcurrencyLimit->new(
max_procs => 1,
path => '/var/run/myapp',
);

my $id = $limit->get_lock;
if (not $id) {
warn "Another process appears to be still running. Exiting.";
exit(0);
}
else {
do_work();
}

# lock released with $limit going out of scope here
}
use 5.14.2;
use DBI;
use DBD::mysql;
use IPC::ConcurrencyLimit;
use IPC::ConcurrencyLimit::Lock::MySQL;

my $limit = IPC::ConcurrencyLimit->new(
type => 'MySQL',
max_procs => 1,
timeout => 2,
make_new_dbh => sub {
DBI->connect("DBI:mysql:database=$database;...")
},
);

# as before:
my $id = $limit->get_lock;
if (not $id) {
# fail
} else {
# success, do work
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store