超时后如何终止 Perl 系统调用? [英] How can I kill a Perl system call after a timeout?

查看:76
本文介绍了超时后如何终止 Perl 系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 脚本,用于运行使用反引号启动的文件处理工具.问题是工具偶尔会挂起,需要将其杀死才能处理其余文件.

I've got a Perl script I'm using for running a file processing tool which is started using backticks. The problem is that occasionally the tool hangs and It needs to be killed in order for the rest of the files to be processed.

应用超时的最佳方法是什么,在此之后父脚本将终止挂起的进程?

Whats the best way best way to apply a timeout after which the parent script will kill the hung process?

目前我正在使用:

foreach $file (@FILES) {
    $runResult = `mytool $file >> $file.log`;
}

但是当 mytool 在 n 秒后挂起时,我希望能够杀死它并继续下一个文件.

But when mytool hangs after n seconds I'd like to be able to kill it and continue to the next file.

推荐答案

我可能不会为此使用 ``.相反,我会使用 |open() 命令,以便它异步运行.这将返回 pid.然后,您可以在睡眠循环中执行非阻塞 wait(),在尝试一定次数但未成功后,向子 pid 发出信号.

I would probably not use `` for this. Instead I would open() the command with | so that it runs asynchronously. This will return the pid. Then you can do a nonblocking wait() in a loop with sleep that after a certain number of tries without success, issues a signal to the child pid.

这篇关于超时后如何终止 Perl 系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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