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

Hello monks, I want my code to execute a command:
`java -jar program.jar`
but i want my perl program to wait for the Java program to return for only, say, 60 seconds. If the Java program did not terminate within 60 seconds, i would like to repeat executing the java program again. Any idea on how I can do this? Thanks,

Replies are listed 'Best First'.
Re: killing a process after some time
by Khen1950fx (Canon) on Jun 11, 2010 at 05:59 UTC
    I used a module that's been around for awhile: Proc::Reliable. It seems to do what you want.
    #!/usr/bin/perl use strict; use warnings; use Proc::Reliable; my $proc = Proc::Reliable->new( 'num_tries' => 5, 'time_per_try' => 60, 'maxtime' => 60); Proc::Reliable::debug(1); my $out = $proc->run('java -jar program.jar'); if($proc->status) { print($proc->stderr); exit; } else { print($proc->stdout); }
Re: killing a process after some time
by ikegami (Patriarch) on Jun 11, 2010 at 05:43 UTC
Re: killing a process after some time
by kejohm (Hermit) on Jun 11, 2010 at 06:21 UTC

    Here is an example from alarm in the perlfunc manpage:

    eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm $timeout; $nread = sysread SOCKET, $buffer, $size; alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't } __END__

    You could probably adapt this to do what you need.

    Update: Link fixed.

Re: killing a process after some time
by eyepopslikeamosquito (Archbishop) on Jun 11, 2010 at 12:09 UTC
Re: killing a process after some time
by DrHyde (Prior) on Jun 11, 2010 at 09:54 UTC
    There's some code in CPAN::FindDependencies::MakeMaker that does pretty much exactly what you want, apart from the repetition bit - but that should be easy to add. Look in the get_reqs_from_mm function.