开发者

Script to restart a process exceeding RAM

开发者 https://www.devze.com 2023-04-10 20:05 出处:网络
I want to write a script which can run as a Windows service. It would restart a process active in the process tabl开发者_如何学JAVAe when the user is using the process and it exceeds for example 500mb

I want to write a script which can run as a Windows service. It would restart a process active in the process tabl开发者_如何学JAVAe when the user is using the process and it exceeds for example 500mb of RAM. How can I do this? Can someone suggest any example?


There's always the dirty way. Using tasklist in Windows:

use List::Util qw<first>;

my ( $mem )      
    = map   { ( my $a = $_ ) =~ s/,//g; $a } 
      grep  {; length } 
      map   { m/([\d,]+)[ ]K$/ } 
      first { m/^perl\.exe\s+$$\b/ } 
      `tasklist`;

if ( $mem > MAX_MEM ) {
    do_something();
}

But as I understand it, you could also do it like this, with Win32::Process::Info:

use Win32::Process::Info;
my  $pi = Win32::Process::Info->new ();
my $set = first { exists $_->{WorkingSetSize} } $pi->GetProcInfo( $$ );
my $mem = $set && $set->{WorkingSetSize};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号