我有一个用例,我必须从 perl 触发 bash 命令,并需要该命令在指定的超时内退出目前我正在使用这个模块
I have a usecase where I have to fire a bash command from perl and need that command to exit within a specified timeout Currently I am using this moudle
use System::Timeout qw(timeout); timeout(10, "my bash script")(由于需要超时,我没有使用 system() 进行调用)
(As timeout is necessary I am not using system() to make the call)
如果 shell 脚本以非零退出代码退出或命令超时,则此函数返回 1.
This function returns 1 if shell script exitted with non-zero exit code or the command timed out.
问题
满足上述两个条件对我来说很重要(我非常了解如何在 python 中执行此操作,但无法获得 perl 的解决方案)
It is important for me to satisfy both of the above criteria(I know very well how to do this in python, but could not get a solution for perl)
我不知道在 perl 中 fork 当前进程然后用 SIGALRM 监视它是否会有所帮助(分叉会给我分叉进程的pid,而不是我从那个分叉启动的bash脚本.是否会杀死分叉,也会杀死它启动的bash进程?)
I do not know if forking a current process in perl and then monitoring it with SIGALRM will help (Forking will give me pid of the forked process and NOT the bash script which I have launched from that fork. Will killing the fork, also kill the bash process it launched?)
感谢您的帮助
推荐答案您的系统可能有 gnu timeout 命令,如果它使用超时杀死子进程并返回否则命令退出代码.如果您没有 gnu timeout,您确实提到您有 bash,这意味着您可以使用我的 bash 模拟器进行 gnu timeout, github/ronaldxs/bash-timeout,我很乐意期待任何反馈.查看 System::Timeout 的源代码,它基于 CPAN 模块 IPC::Cmd,建议将以下内容作为另一个起点:
Your system might have the gnu timeout command which sets an exit code of 124 if it kills the child process with timeout and returns the command exit code otherwise. If you don't have gnu timeout, you did mention you have bash which means you could use my bash emulator for gnu timeout, github/ronaldxs/bash-timeout, and I would happily look forward to any feedback. Looking at the source code for System::Timeout, it is based on CPAN module IPC::Cmd which suggests the following as another starting point:
#!/usr/bin/env perl use Modern::Perl; use Data::Dump; use IPC::Cmd 'run_forked'; my $rc = run_forked('sleep 5; exit 3', { timeout => 2 }); dd $rc;输出:
{ child_pgid => 69066, err_msg => "ran more than [2] seconds\n", exit_code => 0, ... timeout => 2, }更多推荐
在 perl 中启动 bash 命令的超时时间
发布评论