Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Timing out shell commands (paranoia)

by jbert (Priest)
on May 02, 2001 at 15:15 UTC ( #77287=note: print w/replies, xml ) Need Help??

in reply to Timing out shell commands (paranoia)

(As you aleady know...but to give context...)
The 'alarm(X)' function arranges for a signal (SIGALRM) to be delivered to your process (and hence interrupt it) in X seconds (give or take a bit of rounding).

If your process happens to be executing your perl function 'command' then it is interupted and your signal handler called. This then does a 'die' and your eval catching mechanism picks up and carries on.

If you have started another process (this is your case when you have an external command started with system or backticks) then it neither knows nor cares that your process has received an alarm...which is why it continues to run.

I think what you want to do is pretty much in your second example. When you receive the SIGALRM you'll want to kill the child...which means you need the child pid.

Zigster mentioned a good point. 'kill -9' is a bit rude (it depends on what the subprocess does if you care about being rude to it). I differ about the best signal to kill with initially though. The unix default for the 'kill' command is SIGTERM (15), which means 'please die'.

This is all assuming Unix because you mentioned the 'alarm' function. If you want to achieve a similar effect on Win32, the last time I looked at this the best (only?) way was to use the Win32::Process::Create, which creates a handle to the new process upon which you can then wait for a specified period.

Lastly, a quick bit of CPANning shows up:
Proc::Background - Generic interface to Unix and Win32 background process management

"This is a generic interface for placing processes in background on both Unix and Win32 platforms. This module lets you start, kill, wait on, retrieve exit values, and see if background processes still exist."

Which (if it does what it says on the tin) sounds interesting...

  • Comment on Re: Timing out shell commands (paranoia)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://77287]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-24 03:24 GMT
Find Nodes?
    Voting Booth?