linux的OOM改进限制进程内存,在Linux上防止内存不足(OOM)冻结的最佳方法是什么?...

#!/usr/bin/perl -w

# Checks available memory usage and calculates size in MB

# If free memory is below your minimum level specified, then

# the script will attempt to close the troublesome processes down

# that you specify. If it can't, it will issue a -9 KILL signal.

#

# Uses external commands (cat and pidof)

#

# Cheers, insertable

our $memmin = 50;

our @procs = qw(/usr/bin/firefox /usr/local/sbin/apache2);

sub killProcs

{

use vars qw(@procs);

my @pids = ();

foreach $proc (@procs)

{

my $filename=substr($proc, rindex($proc,"/")+1,length($proc)-rindex($proc,"/")-1);

my $pid = `pidof $filename`;

chop($pid);

my @pid = split(/ /,$pid);

push @pids, $pid[0];

}

foreach $pid (@pids)

{

#try to kill process normall first

system("kill -15 " . $pid);

print "Killing " . $pid . "n";

sleep 1;

if (-e "/proc/$pid")

{

print $pid . " is still alive! Issuing a -9 KILL...n";

system("kill -9 " + $pid);

print "Done.n";

} else {

print "Looks like " . $pid . " is deadn";

}

}

print "Successfully finished destroying memory-hogging processes!n";

exit(0);

}

sub checkMem

{

use vars qw($memmin);

my ($free) = $_[0];

if ($free > $memmin)

{

print "Memory usage is OKn";

exit(0);

} else {

killProcs();

}

}

sub main

{

my $meminfo = `cat /proc/meminfo`;

chop($meminfo);

my @meminfo = split(/n/,$meminfo);

foreach my $line (@meminfo)

{

if ($line =~ /^MemFree:s+(.+)skB$/)

{

my $free = ($1 / 1024);

&checkMem($free);

}

}

}

main();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值