cron只运行一个Perl脚本实例 [英] Running only one Perl script instance by cron
问题描述
我需要定期运行Perl脚本(〜每3-5分钟一次)。我想确保只有一个Perl脚本实例将在一段时间内运行,因此下一个循环将不会开始,直到前一个完成。可以/应该通过cron的一些内置功能实现,Perl或I需要在脚本级别处理它?
I need to run Perl script by cron periodically (~every 3-5 minutes). I want to ensure that only one Perl script instance will be running in a time, so next cycle won't start until the previous one is finished. Could/Should that be achieved by some built-in functionality of cron, Perl or I need to handle it at script level?
我对Perl和cron很新,所以请帮助和一般建议。
I am quite new to Perl and cron, so help and general recommendations are appreciated.
推荐答案
我一直有好运气使用 File :: NFSLock 获取脚本本身的独占锁。
I have always had good luck using File::NFSLock to get an exclusive lock on the script itself.
use Fcntl qw(LOCK_EX LOCK_NB);
use File::NFSLock;
# Try to get an exclusive lock on myself.
my $lock = File::NFSLock->new($0, LOCK_EX|LOCK_NB);
die "$0 is already running!\n" unless $lock;
这与其他锁文件建议是一样的,除非我不需要任何除了尝试获取锁定。
This is sort of the same as the other lock file suggestions, except I don't have to do anything except attempt to get the lock.
这篇关于cron只运行一个Perl脚本实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!