<?xml version="1.0" encoding="windows-1252"?>
<node id="998706" title="Re: Windows run an exe print its output and cut it off after a time" created="2012-10-12 10:43:40" updated="2012-10-12 10:43:40">
<type id="11">
note</type>
<author id="749850">
VinsWorldcom</author>
<data>
<field name="doctext">
&lt;p&gt;I too have had issues with alarm() on Windows.  This is a quick workaround - not very efficient or accurate, but close to the timeout you want.  $timeout is number of seconds to run before timing out.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; I need an "infinite output" .exe so the './test.exe' program just prints numbers 1 .. 100000 to the screen.  I set my $timeout for 2 seconds.&lt;/p&gt;

&lt;c&gt;
use strict;
use warnings;

my $pid = open(my $fileHandler, '-|', "./test.exe" );
my $start = time;
my $timeout = 2;

while (&lt;$fileHandler&gt;) {
    print "$_\r"; # \r to not overrun output screen buffer
    last if ((time - $start) &gt;= $timeout);
}

print "\nTimeout! - Time to kill pid: $pid\n"
&lt;/c&gt;

&lt;p&gt;And the output:&lt;/p&gt;

&lt;c&gt;
VinsWorldcom@C:\Users\VinsWorldcom\tmp&gt; TimeThis test.pl

TimeThis :  Command Line :  test.pl
TimeThis :    Start Time :  Fri Oct 12 10:42:04 2012

56464
Timeout! - Time to kill pid: 4192

TimeThis :  Command Line :  test.pl
TimeThis :    Start Time :  Fri Oct 12 10:42:04 2012
TimeThis :      End Time :  Fri Oct 12 10:42:06 2012
TimeThis :  Elapsed Time :  00:00:01.903
&lt;/c&gt;

&lt;p&gt;Of course you could maybe use [cpan://Time::HiRes] to get more accurate.&lt;/p&gt;</field>
<field name="root_node">
998662</field>
<field name="parent_node">
998662</field>
</data>
</node>
