This tutorial on IPC may be useful.

Also, perlfaq8 has one entry titled:

How do I timeout a slow event?

Use the alarm() function, probably in conjunction with a signal handler, as documented in "Signals" in perlipc and the section on ``Signals'' in the Camel. You may instead use the more flexible Sys::AlarmCall module available from CPAN.

Be careful when implementing signals. Take a look at the %SIG section of the perlvar POD for some clues and warnings. Remember that the alarm signal, like other signals, can't be trapped with eval directly, it will exit unconditionally. You have to catch the signal in %SIG and die() to make it catchable. Something like:

$SIG{ALRM} = sub { die 'timeout' };
Now you can catch it with eval.

Another possibility is something like the following (untested) code snippet:

use strict; use IO::Select; open(CMD, "shell_cmd|") or die $!; unless(IO::Select->can_read(1) ) { die "timed out!"; } print while(sysread(CMD, $_, 4096)); close(CMD);

For capturing the output, you probably want (my old favorite), IPC::Open3, which gives you STDIN, STDOUT, and STDERR. Be wary of the race conditions though, read the POD and examples very carefully. Good luck!


In reply to Re: The 'ol shell timeout question by hacker
in thread The 'ol shell timeout question by xiper

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.