Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Process time out

by muppetBoy (Pilgrim)
on May 18, 2000 at 18:15 UTC ( [id://13018]=perlquestion: print w/replies, xml ) Need Help??

muppetBoy has asked for the wisdom of the Perl Monks concerning the following question:

I need to invoke a system process as part of some housekeeping routines. The problem is that it sometimes hangs and doesn't return anything.
Is there a way that I can invoke the process and after, say, 10 minutes if it hasn't returned anything kill the process and raise warnings etc.?
I'm sure there is an obvious solution but I can't think of it.

Replies are listed 'Best First'.
RE: Process time out
by c-era (Curate) on May 18, 2000 at 19:54 UTC
    I would fork the program. Have the child do your tasks and have the parent sleep. After 10 min. you can have the parent kill the child (since you have the pid).
Re: Process time out
by chromatic (Archbishop) on May 18, 2000 at 23:20 UTC
    The alarm code might look something like this:
    $SIG{ALRM} = sub { die "timeout"; }; eval { alarm(60); # shouldn't take more than a minute # do something here alarm(0); # if the last op finished, cancel the alarm }; if ($@ =~ /timeout/) { # you know it didn't finish } else { # continue }
    Shamelessly adapted from the Perl Cookbook, though I once wrote code like this anyway.
Re: Process time out
by BigJoe (Curate) on May 18, 2000 at 20:10 UTC
    You could set up a signal that is just a timer and when it sends the signal it would run a sub to kill the process, warn and start again. Try using the alarm function to set the timer then the sigtrap function to break from the wait().

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-26 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found