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

anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I have a windows exe, which prints some output on windows console and keeps on running for infinite time. I want to put this in a perl script in such a way that when this .exe is executed output for the same is displayed on screen in real time and also it should be stopped/killed after a fixed interval provided as user input. I am able to open the command with:
$pid = open( $fileHandler, '-|', "$Command" );
where $Command has name of my EXE. Then I read $fileHandler and print the records. But I am not able to stop this exe after a fixed interval of time. I tried using alarm, but it is of no use as alarm is not working in windows. Is there any way I can stop this exe after a specified interval?

Replies are listed 'Best First'.
Re: Windows run an exe print its output and cut it off after a time
by kcott (Archbishop) on Oct 12, 2012 at 15:43 UTC

      "Emulated" or not, alarm works fine on Windows. As do both forms of piped open:

      C:\test>perl -E"alarm 5; say(),sleep(1) for 1 .. 10" 1 2 3 4 5 Terminating on signal SIGALRM(14) C:\test>perl -E"$p=open I, '-|', 'ver'; say <I>" Microsoft Windows [Version 6.0.6001] C:\test>perl -E"$p=open O, '|-', 'find \"1\"'; say O for 1 .. 10" 1 10

      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

        I thought alarm did work fine but the OP wrote "... alarm is not working in windows.". As I don't have a Windows Perl in front of me, I checked the doco. I wasn't trying to make any point by using the word emulated - that's just what the doco says: "alarm Emulated using timers ...".

        Scrolling down the perlport page a bit after reading about alarm, I came across the open entry by chance. It seems the doco is just wrong with respect to piped open. perlport says it's "unsupported"; perlfork says it's "not yet implemented".

        Thanks for the example code.

        -- Ken

        Well blow me down, BrowserUK is correct. I've never gotten alarm() to work reliably on Windows - perhaps because I'm not that versed in Signals and didn't know what I was doing or was trying on a system call without the eval/die as described in alarm; however, alarm() added in my original script works:

        use strict; use warnings; my $pid = open(my $fileHandler, '-|', "./test.exe" ); my $DONE = 0; $SIG{ALRM} = sub { $DONE = 1 }; alarm 2; while (<$fileHandler>) { print "$_\r"; # \r to not overrun output screen buffer last if ($DONE) } 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 13:01:09 2012 114601 Timeout! - Time to kill pid: 4700 TimeThis : Command Line : test.pl TimeThis : Start Time : Fri Oct 12 13:01:09 2012 TimeThis : End Time : Fri Oct 12 13:01:12 2012 TimeThis : Elapsed Time : 00:00:02.454
Re: Windows run an exe print its output and cut it off after a time
by VinsWorldcom (Prior) on Oct 12, 2012 at 14:43 UTC

    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.

      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