|Don't ask to ask, just ask|
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??|
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:
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:
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):
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'
In Section Seekers of Perl Wisdom