Perl,在 x 秒后使脚本超时? [英] Perl, make script timeout after x number of seconds?
本文介绍了Perl,在 x 秒后使脚本超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在搜索这个问题,但令人惊讶的是很难得到一个直接的答案(因为 php 似乎有更多关于这个主题的信息).我需要让我的 perl 脚本在指定的数字后死亡秒,因为现在它们运行时间太长并阻塞了我的系统,我该如何做到让整个脚本在指定的秒数后停止?
I have been searching on this but it is surprisingly hard to come by a straight answer to this (as php has a lot more info on this topic it seems).. I need to make my perl script die after a specified number of seconds because, as it is now, they are running too long and clogging up my system, how can I make it so the entire script just dies after a specified number of seconds?
我知道终止脚本的外部解决方案,但我想从 perl 脚本本身中完成.
I know about external solutions to kill the script but I would like to do it from within the perl script itself.
谢谢
推荐答案
[sinan@kas ~]$ cat t.pl
#!/usr/bin/perl
use strict; use warnings;
use Try::Tiny;
try {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 5;
main();
alarm 0;
}
catch {
die $_ unless $_ eq "alarm\n";
print "timed out\n";
};
print "done\n";
sub main {
sleep 20;
}
输出:
[sinan@kas ~]$ time perl t.pl
timed out
done
real 0m5.029s
user 0m0.027s
sys 0m0.000s
这篇关于Perl,在 x 秒后使脚本超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文