Killing a process that takes too long



Hello all,

I want to make a system call, and then kill the process if it takes
too long. This seems like a common enough problem, so I read perldoc
-q timeout, which says to look into the alarm () function. I read
some examples of using alarm in the camel book and the o'reilly perl
cookbook, but I'm still having some trouble. The alarm clock goes off
properly when it's supposed to, but my problem is subsequently killing
the process I created. My example:

use warnings;
use strict;

eval {
local $SIG{ALRM} = sub {
print "Timed out\n";
die 'alarm';
};
alarm 5;
system ('sleep 45');
alarm 0;
};
die if $@ && $@ !~ /alarm/;
print "Exited normally.\n";

__END__

This is the output (I'm on a linux machine; '>' represents the prompt):
perl test.pl
Timed out
Exited normally.
ps -af
[...]
root 31498 1 0 12:30 pts/2 00:00:00 sleep 45
[...]

So, if I do a ps -af, I can see that my perl script is a goner, but
the process spawned from the system call is still alive.

If I could figure out the process ID, I could kill it in the
$SIG{ALRM} anonymous subroutine.

Any suggestions? Would you use a different strategy in my situation?

Thanks for any help!
- Jen
.



Relevant Pages

  • Re: [PM] Patrick: which part of "maintainer" and "peer review" needs explaining
    ... Anyway, if you killed sleep, you should kill ... I will fix alarm. ... others) that abstract the mechanism for entering sleep away from the user. ...
    (Linux-Kernel)
  • Re: How to kill a perl thread
    ... First, to kill the perl thread, you must somehow return from ... it's code block, either with a return, or it reaching the end of ... to kill the $pid after $waitTime. ... You might be able to set up an alarm on the eval, ...
    (comp.lang.perl.misc)
  • Re: how to TIMEOUT a system()?
    ... weirong@capsl.udel.edu (Weirong Zhu) wrote: ... > alarm 5; ... But I want to kill it when it's timed out. ... the pid of the child process, and then kill it in the signal handler. ...
    (comp.lang.perl.misc)
  • Re: (Newbie) Timed operations with eval/die
    ... > alarm 10; ## for example ... > Specifically, I want to spawn child processes and wait for them, but ... > parent will try to kill a PID which was already "reaped". ... I am safe. ...
    (comp.lang.perl.misc)
  • Re: RFD: create unmoderated newsgroup uk.tech.heating
    ... >> of question is fairly common and is often asked when the poster is ... Feel free,, every independent evaluation of Corgi servicing of gas ... >> worked or the effectiveness of alarm systems in general you would ... >wing of heating, as it is often associated with heating systems. ...
    (uk.net.news.config)