如何在 Perl 中终止带有警报的系统命令? [英] How can I terminate a system command with alarm in Perl?

查看:31
本文介绍了如何在 Perl 中终止带有警报的系统命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上运行以下代码片段.从客户端读取后,服务器开始持续侦听.我想在一段时间后终止此​​命令.

I am running the below code snippet on Windows. The server starts listening continuously after reading from client. I want to terminate this command after a time period.

如果我使用 alarm() 函数调用 main.pl,然后它终止整个 Perl 程序(这里是 main.pl),所以我称之为 system 命令,将其放置在单独的 Perl 文件中并使用 alarm.pl)>system 命令.

If I use alarm() function call within main.pl, then it terminates the whole Perl program (here main.pl), so I called this system command by placing it in a separate Perl file and calling this Perl file (alarm.pl) in the original Perl File using the system command.

但是这样我既无法在原始 Perl 文件中也无法在调用的一个 Perl 文件中获取此 system() 调用的输出.

But in this way I was unable to take the output of this system() call neither in the original Perl File nor in called one Perl File.

谁能告诉我终止 system() 调用的方法或以我上面使用的方式获取输出?

Could anybody please let me know the way to terminate a system() call or take the output in that way I used above?

my @output = system("alarm.pl");
print"one iperf completed
";

open FILE, ">display.txt" or die $!; 
print FILE @output_1; 
close FILE;

警报.pl

alarm 30;
my @output_1 = readpipe("adb shell cd /data/app; ./iperf -u -s -p 5001");

open FILE, ">display.txt" or die $!; 
print FILE @output_1; 
close FILE;

在这两种情况下,display.txt 总是空的.

In both ways display.txt is always empty.

推荐答案

这里有几个单独的问题.

There are a few separate issues here.

首先,为了防止 alarm 杀死您的脚本,您需要处理警报信号.请参阅警报 文档.您不应该为此需要两个脚本.

First, to keep the alarm from killing your script, you need to handle the ALRM signal. See the alarm documentation. You shouldn't need two scripts for this.

第二,system 不捕获输出.如果你想这样做,你需要一个反引号变体或一个管道.Stackoverflow 上已经有答案了.

Second, system doesn't capture output. You need one of the backtick variants or a pipe if you want to do that. There are answers for that on Stackoverflow already.

第三,如果 alarm.pldisplay.txt 中放了任何东西,当你重新打开文件处于写入模式.您只需在一处创建文件.去掉多余的脚本,就不会有这个问题了.

Third, if alarm.pl puts anything in display.txt, you discard it in main.pl when you re-open the file in write mode. You only need to create the file in one place. When you get rid of the extra script, you won't have this problem.

我最近在使用 alarmsystem 时遇到了一些问题,但是 切换到 IPC::System::Simple 解决了这个问题.

I recently had some problems with alarm and system, but switching to IPC::System::Simple fixed that.

祝你好运:)

这篇关于如何在 Perl 中终止带有警报的系统命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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