Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Windows run an exe print its output and cut it off after a time

by VinsWorldcom (Priest)
on Oct 12, 2012 at 14:43 UTC ( #998706=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Windows run an exe print its output and cut it off after a time
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998706]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2014-09-15 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls