Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: The 'ol shell timeout question

by hacker (Priest)
on Jun 18, 2003 at 02:24 UTC ( [id://266705]=note: print w/replies, xml ) Need Help??


in reply to The 'ol shell timeout question

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://266705]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found