Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found