sri1230 has asked for the wisdom of the Perl Monks concerning the following question:

I am tryign to use this code to kill java.exe processes over a certain memory usage on a windows 2003 server. The script runs fine but does not kill the process. What is it that i am doing wrong?
use strict; use Win32::Process::Info; use Win32::Process; use Data::Dumper; Win32::Process::Info->Set(variant=>'WMI'); my $pi = Win32::Process::Info->new(); my $exitcode; for($pi->ListPids){ my ($info) = $pi->GetProcInfo($_); if($info->{Caption} =~ m/^java.exe$/i){ if($info->{WorkingSetSize} ge '100000000'){ my $obj; print "Kiling Java Process\n"; my $pid = $info->{ProcessId}; Win32::Process::Open($obj,$pid,1); $obj->Kill(0); } } }

Replies are listed 'Best First'.
Re: Kill java processes on Windows server
by BrowserUk (Patriarch) on Jan 21, 2010 at 22:17 UTC

    What do you get it you add:

    $obj->Kill(0) or die $^E;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi got this error when i did that The handle is invalid at kill_process.pl line 16. here is the code used
      use strict; use Win32::Process::Info; use Win32::Process; use Data::Dumper; Win32::Process::Info->Set(variant=>'WMI'); my $pi = Win32::Process::Info->new(); my $exitcode; for($pi->ListPids){ my ($info) = $pi->GetProcInfo($_); if($info->{Caption} =~ m/^java.exe$/i){ if($info->{WorkingSetSize} ge '100000000'){ my $obj; print "Kiling Java Process\n"; my $pid = $info->{ProcessId}; Win32::Process::Open($obj,$pid,1); $obj->Kill(0) or die $^E; } } }

        Then I suspect that you do not have the appropriate permissions to kill those java processes.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.