Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Mechanism for ensuring only one instance of a Perl script can only run?

by stevieb (Canon)
on Dec 02, 2022 at 16:41 UTC ( #11148502=note: print w/replies, xml ) Need Help??


in reply to Mechanism for ensuring only one instance of a Perl script can only run?

I wrote Script::Singleton exactly for this task.

It uses the shared memory functionality of IPC::Shareable which I also maintain.

To use it, you literally only need to use it:

use Script::Singleton;

...done.

It uses the script's path and filename as the "glue" aka. shared memory key for identification. If you want to use a custom glue:

use Script::Singleton glue => 'UNIQUE GLUE STRING';

Best part I like about it is that if there's a system interruption or reboot, you don't have to worry about lock files hanging around.

Here's an example (using the module with the warn parameter which is false by default):

use warnings; use strict; use Script::Singleton warn => 1; sleep 5;

Run the script in one CLI, and within five seconds, run it in a second window and you'll get:

Process ID 24891 exited due to exclusive shared memory collision at segment/semaphore key '0x40d7106c'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-03-26 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (63 votes). Check out past polls.

    Notices?