Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
Hi,

I would like to call an external programm (on Windows, Perl v 5.10.0 and cannot update or install modules - I use hello.pl as example), capture the output into a var and kill the programm if it does not come back withhin e.g. 60 seconds (and go on with my perl script after the kill). Especially the killing part is the challenge for me and do not know how to solve it the best way.

test.pl

use strict; use warnings; my $cmd_1 = "perl hello.pl"; open (CMD, "$cmd_1 2>&1|") or warn ("!!! Can't run program: $!\n"); while (my $line = <CMD>) { print $line; #if ($line =~ m/^xyz/) #{ # # do some string manipulation and write it to another file #} } close CMD;

hello.pl

use strict; use warnings; $| = 1; print "Hello World\n"; print "Sleeping 1000 seconds\n"; for (1 .. 1000) { sleep 1; print ".\n"; }

I already had a look into a few examples with Win32::Process and the killing works fine with that but did not get the output captured (stdout/stderr). Thanks for any help, hints and code examples.

kind regards de Michi

In reply to Run and kill external programm after x seconds by demichi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.