#!/usr/bin/perl use strict; use warnings; use Parallel::ForkManager; use constant MAX_PROCS => 1; use constant MAX_TIME => 10; my ($pid, $ident, $start); my $pm = new Parallel::ForkManager( MAX_PROCS ); @SIG{ qw(ABRT STOP TERM INT) } = (sub { kill 9, $pid; exit }) x 4; $pm->run_on_start ( sub { ($pid, $ident)= @_; $start = time; print "$ident started at ", scalar localtime($start), " with pid: $pid\n"; } ); $pm->run_on_wait ( sub { if ( time - $start > MAX_TIME ) { print "Killing $pid - taking too long to run\n"; kill 9, $pid; } }, 1 ); $pm->run_on_finish ( sub { ($pid, my $exit_code, $ident) = @_; print "$ident ($pid) just completed with : $exit_code\n"; } ); while ( 1 ) { $pid = $pm->start( 'health check' ) and next; # We are in child process for ( 1 .. rand 15 ) { print "I am the child doing work\n"; sleep 1; } $pm->finish; # We are in parent process }