Perl – How to kill a process tree in Windows

killperlprocesswinapi

Hi i have this process tree:

enter image description here

The above screenshot shows a process tree. In my Perl script i know the PID of dscli. I have written the following code to kill a single PID:

use Win32::Process;
use strict;
use warnings;

if(defined($ARGV[0])){
    my $pid = "$ARGV[0]";
    my $exitcode = 0;
    Win32::Process::KillProcess($pid, $exitcode);
}else{
    print "No argument provided :(\n";
}

The problem is that in my script i don't know the java process' PID. I have to get the dscli's child PID which is the java process. If i kill the dscli's PID using the above code then the child(java) don't die with it.

So my question is, how can i kill the java process which is the child of dscli using perl?

Best Answer

You can use the Windows command TASKKILL /T to terminate a process and its child processes.

$pid = ...;
system("TASKKILL /F /T /PID $pid");