Erroring Out When Creating A Hung Process

T

tpurl

There's a process that I open in a script using the following code:

my $cmd = "myproc -l |";
open(MYPROC, $cmd) or croak("Error $?: Couldn't execute $cmd: $!");
my @lines = <MYPROC>;
close(MYPROC);

This code works pretty well *unless* the process hangs, which is does
occasionally. If myproc hangs, then my script hangs.

Is there a way to open a process and raise an error if it runs for more
than, say 15 seconds?

Thanks in advance for any help!

Tom Purl
 
U

usenet

tpurl said:
Is there a way to open a process and raise an error if it runs for more
than, say 15 seconds?

You can fork a child process and let the parent monitor it.

perldoc -q fork
perldoc -q background
 
A

Anno Siegel

You can fork a child process and let the parent monitor it.

perldoc -q fork
perldoc -q background

He's already forking,

open ..., "myproc -l |";

implies a fork. Monitoring by the parent would have to be done
concurrently with reading from the child process, otherwise the
child may block (and appear to be running for a long time) because
it can't deliver its output.

The code can remain as it is, but the parent should set an alarm timer
and a handler that kills the child process. Look in perlipc under
signals for the non-trivial details.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top