Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Kill system process after timeout

New User

Dec 8, 2014, 2:08 AM

Views: 34278
Kill system process after timeout

I need a perl code that runs a Dos program with a timeout and kills the program if the timeout is reached. For the timeout part, I was able to find this code (and it works too). But this doesn't kill the Dos program after timeout is reached:

my ($XfoilLocation, $ResultsDirectory, $basename, $timeout) = @ARGV; 

my $stringA = $XfoilLocation . ' <' . $ResultsDirectory . '\\' . $basename . '.xfoil >nul';

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

For killing the Dos program based on its PID, I found this piece of code:

my $cpid; # Run Xfoil and get the PID of this instance of Xfoil 
defined($cpid = fork) or die $!;
$cpid and last;
echo $cpid

Now, can someone please help me in combining the two? Because just pasting the second code into the first one doesn't work.

For killing a process via its PID I can use:

my $stringC = 'taskkill /f /pid ' . $cpid; 

Any help is highly appreciated.

(This post was edited by Aminxp20 on Dec 8, 2014, 2:13 AM)