This was the only module I had in mind, and did not take time to test it out before posting.
The module is great, and solves easily most of my problems.
Here is the code I wrote :
use warnings;
use strict;
use Data::Dumper;
use Win32::Job;
my $cmd = "sleep 20";
my ($min_time,$max_time,$kill_time) = (12,15,30);
my $i=1;
my $max_sent = 0;
my $job = Win32::Job->new;
my $pid = $job->spawn(undef, $cmd, { stdin => 'NUL', stdout => 'stdou
+t.log', stderr => 'stdout.log'});
print "PID = $pid\n";
my $ok = $job->watch("handler", 1);
if($ok) { print "Done\n" ; }
else { print "Problem !\n"; }
my $status = $job->status;
print Dumper($status);
if($i<=$min_time) {
print "MIN_TIME Too short...\n";
}
sub handler {
print "$pid still alive after $i sec\n";
$i++;
if($i>$max_time and $max_sent==0) {
print "MAX_TIME Too long...\n";
$max_sent = 1;
}
if($i>$kill_time) {
print "KILL TIME : kill...\n";
return 1;
}
return 0;
}
Now... same issue as before. I don't manage to distinguish the fact that the process launched was killed externally, or finished with exit-code 1. Any idea about that ?
Also, what if my perl process is killed (by the task manager for example) ? I tried :
$SIG{INT} = sub { die "Caught a INT $!" };
$SIG{ABRT} = sub { die "Caught a SIGABRT $!" };
$SIG{FPE} = sub { die "Caught a SIGFPE $!" };
$SIG{ILL} = sub { die "Caught a SIGILL $!" };
$SIG{INT} = sub { die "Caught a SIGINT $!" };
$SIG{SEGV} = sub { die "Caught a SIGSEGV $!" };
$SIG{TERM} = sub { die "Caught a SIGTERM $!" };
But none is actually called. Any idea ? |