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'In reply to Re: Mechanism for ensuring only one instance of a Perl script can only run?
by stevieb
in thread Mechanism for ensuring only one instance of a Perl script can only run?
by redapplesonly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |