Killing a process that takes too long
- From: jen.spinney@xxxxxxxxx (Jen Spinney)
- Date: Tue, 21 Nov 2006 13:07:02 -0500
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.plTimed 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
.
- Follow-Ups:
- Re: Killing a process that takes too long
- From: Tom Phoenix
- Re: Killing a process that takes too long
- From: Paul Lalli
- Re: Killing a process that takes too long
- Prev by Date: Re: commit
- Next by Date: Re: Killing a process that takes too long
- Previous by thread: RE: Hello to Perl World (U)
- Next by thread: Re: Killing a process that takes too long
- Index(es):
Relevant Pages
|