Use File::lockfile in Perl to ensure that a script instance runs _perl

Source: Internet
Author: User

Write some monitoring scripts in Perl and put them in crontab to schedule execution. It is sometimes found that a script runs too long and runs multiple instances at the same time, so it is necessary to control the script and run only one instance.

The simplest natural idea is to check and create an empty lock file in the script and delete it at the end of the script. Determine whether the script is running by determining the existence of the file. However, there is a bug that if the script runs abnormally terminated, the lock file is not removed properly, causing the script to run again.

Empty lock file, then consider adding a bit of content in the lock file, such as the process of the PID number, and then check whether the process of the PID number is still running, you can avoid the above bug. There are a number of out-of-the-box modules on the CPAN that can accomplish these functions, such as File::lockfile, File::P ID, Proc::P id::file.

Here's an example of File::lockfile, very simple:

The following is a code fragment:

Copy Code code as follows:

#!/usr/bin/perl-w
Usefile::lockfile;
# lock file is in/tmp directory, named Test_file_lock.lck
My $lockfile = file::lockfile->new (' Test_file_lock ', '/tmp ');
# Check if the script is running, exit if it is already running
if (My $pid = $lockfile->check) {
Print "program are already running with PID: $pid";
Exit
}
#更新lock文件
$lockfile->write;
# Scripting Logic
Sleep30
#删除lock文件
$lockfile->remove;

By looking at FILE/LOCKFILE.PM's source code, you can see if the process that is logged in the lock file is running, simply through the kill-0 $pid can be achieved. So even without the above modules, it is also very easy to implement yourself.

Summary:

This method is often used in the script to limit the single instance of the method, MySQL and other programs before each boot will also check the last legacy of mysql.pid files.

Another method is to lock the lock file and determine if there is a lock to ensure uniqueness.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.