See system call with time out. There are other ways too, if you want to use an event loop, or thread for a timer, but alarm is usually what you are after. An old example:
#!/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.
| [reply] [d/l] |
...unless i manually kill (Ctrl+Alt+Del) ...
Are you on Windows? If so, run your external program with Win32::Job, which has timeout support built in.
use Win32::Job;
my $job = Win32::Job->new;
# Run 'perl Makefile.PL' for 10 seconds
$job->spawn($Config{perlpath}, "perl Makefile.PL");
$job->run(10);
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [d/l] |
What have you tried so far that didn't work the way you wanted? | [reply] |