【发布时间】:2014-01-20 22:23:45
【问题描述】:
我浏览并阅读了其他一些关于在一段时间后让命令停止运行的问题,但我尝试的一切都不起作用。
该命令的行为就像它想在指定的时间后停止一样(它返回终端提示符一秒钟)但只是继续运行并且不会停止。
这是我的代码:
#!/usr/bin/perl
use strict;
use warnings;
use Try::Tiny;
try {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 5;
system("ping 192.168.1.1");
alarm 0;
}
catch {
die $_ unless $_ eq "alarm\n";
print "timed out\n";
};
exit;
终端中的输出如下所示:
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.98 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=3.13 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=3.20 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=3.17 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=3.16 ms
64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=3.11 ms
64 bytes from 192.168.1.1: icmp_seq=7 ttl=64 time=3.18 ms
64 bytes from 192.168.1.1: icmp_seq=8 ttl=64 time=3.20 ms
64 bytes from 192.168.1.1: icmp_seq=9 ttl=64 time=3.22 ms
64 bytes from 192.168.1.1: icmp_seq=10 ttl=64 time=3.20 ms
skilo@hp-xubuntu:~/perlstuff$ 64 bytes from 192.168.1.1: icmp_seq=11 ttl=64 time=3.06 ms
64 bytes from 192.168.1.1: icmp_seq=12 ttl=64 time=3.19 ms
64 bytes from 192.168.1.1: icmp_seq=13 ttl=64 time=3.21 ms
64 bytes from 192.168.1.1: icmp_seq=14 ttl=64 time=3.21 ms
注意它如何试图停止但不能。
【问题讨论】:
-
您是否在
catch块中打印了$_? -
它会在 Windows 上为我打印
timed out\n,但之后会继续 ping。很有趣。 -
是的,它只是在提示上方打印“警报”一秒钟然后继续。
-
stackoverflow.com/a/3763712/1331451 表示这个问题是已知的,也给出了解决方案。
标签: perl