Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a process tree in Windows

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?

like image 733
Diemauerdk Avatar asked Jun 04 '12 12:06

Diemauerdk


People also ask

How do you kill a process tree?

Using htop , you can use F5 to show the process tree's. If you select the process at the top of the tree you want kill, then press F9 followed by Enter it will close the process and the entire process tree in one go. In the screen shot below this action would cause Chrome and all sub process to be closed.

How do I kill a PID in Windows?

To kill the process using PID For Example - To kill Notepad, run the command as, taskkill /PID 2404 /F, where /F is used to kill the process forcefully.

How do I end a process tree in CMD?

You might can use tasklist to get the process ID of the process you want to target and then use taskkill /F /PID <PID> to kill it.


2 Answers

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

$pid = ...;
system("TASKKILL /F /T /PID $pid");
like image 69
mob Avatar answered Sep 17 '22 23:09

mob


It's possible to use WMI from PERL. WMI is able to find the PID of all child processes of a given parent. Note the query "select * from win32_process where ParentProcessId={0}". If you have the list of child PIDs, you can call Win32::Process::KillProcess.

like image 34
kol Avatar answered Sep 17 '22 23:09

kol