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

查看:79
本文介绍了如何在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.

如果我在main.pl中使用 alarm()函数调用,则它将终止整个Perl程序(此处为main.pl),因此我通过放置以下命令来将此 system 命令在单独的Perl文件中 并使用 system 命令在原始Perl文件中调用此Perl文件(alarm.pl)

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\n";

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

alarm.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始终为空.

推荐答案

这里有一些单独的问题.

There are a few separate issues here.

首先,要防止警报杀死脚本,您需要处理ALRM信号.请参阅报警文档.您不需要两个脚本.

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.

第二,系统无法捕获输出.如果要这样做,则需要反引号变体之一或管道.已经在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.pl 将任何内容放入 display.txt 中,则在重新打开菜单时将其丢弃在 main.pl 中.文件处于写入模式.您只需要在一个地方创建文件.当您删除多余的脚本时,就不会有此问题.

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上遇到了一些问题,但是

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

祝你好运,:)

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

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