http://www.perlmonks.org?node_id=998706


in reply to Windows run an exe print its output and cut it off after a time

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.

NOTE: 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.

use strict; use warnings; my $pid = open(my $fileHandler, '-|', "./test.exe" ); my $start = time; my $timeout = 2; while (<$fileHandler>) { print "$_\r"; # \r to not overrun output screen buffer last if ((time - $start) >= $timeout); } print "\nTimeout! - Time to kill pid: $pid\n"

And the output:

VinsWorldcom@C:\Users\VinsWorldcom\tmp> 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

Of course you could maybe use Time::HiRes to get more accurate.

Replies are listed 'Best First'.
Re^2: Windows run an exe print its output and cut it off after a time
by anshumangoyal (Scribe) on Oct 13, 2012 at 06:41 UTC
    What will happen if that exe hangs and stops giving output. This program will go in endless loop always..

      alarm won't interrupt an in-progress read -- on Windows or *nix -- if SAFE_SIGNALS are in force.

      If you want a solution, you need to start giving feedback. For instance, you never did respond to my request for further information the last time you asked this question.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong