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

I'm looking for a way to run a Windows executable repeatedly with varying commandline options using Active Perl 5.10. The problem I ran into was that the executable will sometimes hang and I was not able to find a way to get it to time out on its own in this case. I was able to get it run with:

use Win32::Process; Win32::Process::Create($processobj, "C:\\foo.exe", "foo -options_here" +, 0, NORMAL_PRIORITY_CLASS, ".") || die print Win32::FormatMessage( W +in32::GetLastError() ); $processobj->Wait(20000); $processobj->GetExitCode($return);

This works and does what I want it to except I have no means of capturing standard out (other than redirecting everything to a file). I've read the other posts about using cmd.exe and redirecting that to a file, but I need to be able to process standard out in the rest of the script for logging to make any sense. Is there a way to do this, or maybe some better completely different way to run windows executables with the ability to time out the process? I've tried the following as suggested elsewhere and it never actually timed out:

eval { local $SIG{ALRM} = sub {die "alarm";}; alarm(20); $return = `foo -options_here`; alarm(0); }; if ($@) { if ($@ =~ /alarm/) { print "timeout\n"; } else { die; } }

I know its anathema to perl gurus, but I really don't care mucha bout zombie processes and handling everything cleanly as I'm just running this on a laptop. I really just need to be able to report where the executable is running fully and where its failing.

Replies are listed 'Best First'.
Re: Win32::Process output
by BrowserUk (Patriarch) on Nov 03, 2010 at 18:20 UTC

      OK so the syntax on that one is far beyond me. The idea though is something like this?

      use threads; $thread = $threads->new(\&dostuff, "options_here"); $thread->detach; sub dostuff { $return = `foo.exe -$_[0]`; print $return; do_logging_stuff($return); }

      I would need to limit the number of threads somehow though? No clue how to do this. And while I don't really care much if there are a few threads running about not completing, is there a (syntactically simple) method to kill threads that live longer than a certain period of time?

        Try this (from a couple of posts down from the link I provided at:633867):

        #! perl -slw use strict; sub timedCommand { use threads; use threads::shared; my( $cmd, $timeout ) = @_; my @results :shared; my $pid :shared; async { $pid = open my $fh, "$cmd |" or die "$!, $^E"; @results = <$fh>; }->detach; kill 0, $pid while sleep 1 and $timeout--; kill 3, $pid and return if $timeout; return @results; } ... ## Run foo.exe, kill it after 20 seconds. my @results = timedCommand( 'foo.exe -opt', 20 ); ...

        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.
Re: Win32::Process output
by Anonymous Monk on Nov 03, 2010 at 21:43 UTC

    I have used Win32::Job successfully in this scenario. Should work well for you too since you seem to be on the Win platform.