Reputation: 5958
Hi i have this process tree:
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?
Upvotes: 10
Views: 8987
Reputation: 2243
I'd also suggest using WMI, but you might want to just call a VBScript from your Perl. Here is a script that I use to kill by command line, which can help you narrow down a specific java process based on other things that were in the command line when it was launched:
If WScript.Arguments.Count = 1 Then
strProcess = WScript.Arguments.Item(0)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("WScript.Shell")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process")
If colProcessList.Count > 0 Then
For Each objItem in colProcessList
If InStr(objItem.CommandLine, strProcess) Then
If (InStr(objItem.CommandLine, "cscript")) Then
Else
WScript.StdOut.Write objItem.Name + " "
objItem.Terminate()
End If
End If
Next
Else
WScript.StdOut.Write "No instances found running"
End If
Else
WScript.StdOut.Write "Bad Arguments"
End If
Run it like this:
CScript whatEverYouNameIt.vbs "somethingInCommandLineLikeAClassName"
Upvotes: 0
Reputation: 118695
You can use the Windows command TASKKILL /T
to terminate a process and its child processes.
$pid = ...;
system("TASKKILL /F /T /PID $pid");
Upvotes: 11
Reputation: 28728
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
.
Upvotes: 3