how to limit system() execution time?

A

Adrian

Hi,

I'm shelling out to a unreliable 3rd party utility that
is supposed to spit some stuff out to STDOUT and exit.

It always spits out the right data but sometimes it just
doesn't exit properly and the system() call is left
hanging there..

is there any easy way (through threads perhaps?) of
giving the system() call say 10 seconds to do it's thing,
capturing the STDOUT as usual, but then killing it off if
it hasn't exited?

Thanks,
Adrian.
 
J

Jürgen Exner

Adrian wrote:
[...]
is there any easy way (through threads perhaps?) of
giving the system() call say 10 seconds to do it's thing,
capturing the STDOUT as usual, but then killing it off if
it hasn't exited?

Did you check "perldoc -q timeout"?

jue
 
D

Damian James

Adrian wrote:
[...]
is there any easy way (through threads perhaps?) of
giving the system() call say 10 seconds to do it's thing,
capturing the STDOUT as usual, but then killing it off if
it hasn't exited?

Did you check "perldoc -q timeout"?

Hmm. The first thing that occurred to me was "Do the fork()
and exec() manually, capture the pid of the child, kill it
in a SIGALRM handler."

Interestingly, the FAQ your refer to itself refers to perlipc,
which in turn talks about SIGALRM being caught in an eval block,
and says:

If the operation being timed out is system() or qx(), this technique is
liable to generate zombies. If this matters to you, you'll need to
do your own fork() and exec(), and kill the errant child process.

Now I don't know if zombies matter to the OP, nor do I quite
understand how he is capturing STDOUT using system rather than,
for instance qx// or open.

Anyhow, I'd suggest the OP look at the "Safe Pipe Opens" section
in perlipc, look at the various examples and consider setting a
SIGALRM handler and calling alarm(10) at the appropriate place.

--damian
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top