【发布时间】:2017-10-31 08:58:34
【问题描述】:
我正在尝试实现一个例程,该例程将接受“命令”和相关的“超时”。 如果命令在指定时间内完成,它应该返回输出。 否则 - 它应该终止进程。
sub runWithTimeout {
my ($pCommand,$pTimeOut) = @_;
my (@aResult);
print "Executing command [$pCommand] with timeout [$pTimeOut] sec/s \n";
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $pTimeOut;
@aResult = `$pCommand`;
alarm 0;
};
if ($@) {
print("Command [$pCommand] timed out\n");
# Need to kill the process.However I don't have the PID here.
# kill -9 pid
} else {
print "Command completed\n";
#print Dumper(\@aResult);
}
}
示例调用:
&runWithTimeout('ls -lrt',5);
Executing command [ls -lrt] with timeout [5] sec/s
Command completed
&runWithTimeout('sleep 10;ls -lrt',5);
Executing command [sleep 10;ls -lrt] with timeout [5] sec/s
Command [sleep 10;ls -lrt] timed out
猜猜我是否有 PID - 我可以在 if 块中对 PID 使用“kill”。
任何关于如何获得 PID(或任何其他更好的方法)的指针 - 这将是一个很大的帮助。
【问题讨论】:
-
也许在 perl 中不这样做会更容易,而是使用
timeout命令来代替? -
最简单的IPC::Run
标签: perl timeout kill-process backticks