in reply to killing a program called with system() if it takes too long?
#!/usr/bin/perl use strict; use warnings; my $timeout = 180; my @array = ( "command 1 goes here", "command 2 goes here", "command 3 goes here", ); for (@array) { eval { local $SIG{ALRM} = sub { die "alarm\n"; }; alarm $timeout; system($_); alarm 0; }; } # abigail says # Don't do it with system directly, or you might generate zombies. # fork(), do an exec() in the child, and then, in an eval block, # setup an alarm handler that does a die(), set the alarm, # waitpid() for your child, cancel the alarm. Outside the eval # block, check for $@, if it indicates the alarm was triggered, # kill the child and waitpid() for it. # No doubt there are some race conditions left in, but it's a start.
|
|---|