我一直在寻找这个,但很难得到一个直接的答案(因为PHP似乎有很多关于这个主题的信息)..我需要让我的perl脚本在指定的秒数后死亡,因为,就像现在一样,它们运行时间太长而且堵塞了我的系统,我怎么能这样做才能使整个脚本在指定的秒数后死掉?
我知道杀死脚本的外部解决方案,但我想在perl脚本本身内完成.
谢谢
Sin*_*nür 12
[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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
[sinan@kas ~]$ time perl t.pl timed out done real 0m5.029s user 0m0.027s sys 0m0.000s