cron 只运行一个 Perl 脚本实例 [英] Running only one Perl script instance by cron

查看:28
本文介绍了cron 只运行一个 Perl 脚本实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定期(~每 3-5 分钟)通过 cron 运行 Perl 脚本.我想确保一次只运行一个 Perl 脚本实例,所以下一个周期不会开始,直到前一个周期完成.这可以/应该通过 cron、Perl 的某些内置功能来实现,还是我需要在脚本级别处理它?

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!
" 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆