It is sometimes necessary to write a Perl script that runs only
if this same script is not working. I.e., to guarantee that
at one point of time only one instance of this script.
For example, this problem may occur if the script is run for the crown
every minute and sometimes it can run more than a minute.
Here we are talking about the case when the script works on one server and you want to ensure
execution is not more than one instance of the script on the same machine.
The easiest way to solve this problem is to use the Perl library
The library comes with Perl, it needs to be installed additionally.
To make a script that only works in one instance, you need to add
in the existing script
use Sys::RunAlone; and
Here is an example of a script that works only in one instance:
If you run this script, it displays the text
working and 10 seconds to hang
and wait, and then exits.
If you open another console and while the script is running to try to run it one more time
this run will fail, and no code from the script will not be executed:
$ perl script.pl
A copy of 'script.pl' is already running
__END__ in the script is mandatory. Library
Sys::RunAlone uses this
the unit in order to take Locke. And if this block is not, and the library
the script will not work, the screen will display error
Add __END__ to end of script 'script.pl' to be able use the features of Sys::RunALone.