检查以下脚本:
系统内存用尽时需要应用程序/脚本警报
#!/bin/bash
#Minimum available memory limit, MB
THRESHOLD=400
#Check time interval, sec
INTERVAL=30
while :
do
free=$(free -m|awk '/^Mem:/{print $4}')
buffers=$(free -m|awk '/^Mem:/{print $6}')
cached=$(free -m|awk '/^Mem:/{print $7}')
available=$(free -m | awk '/^-\/+/{print $4}')
message="Free $free""MB"", buffers $buffers""MB"", cached $cached""MB"", available $available""MB"""
if [ $available -lt $THRESHOLD ]
then
notify-send "Memory is running out!" "$message"
fi
echo $message
sleep $INTERVAL
done
PHP:
#!/usr/bin/php
<?php
$alert_percent=($argc>1)?(int)$argv[1]:90;
//$interval=($argc>2):(int)$argv[2]:25;
//while(true)
//{
exec("free",$free);
$free=implode(' ',$free);
preg_match_all("/(?<=\s)\d+/",$free,$match);
list($total_mem,$used_mem,$free_mem,$shared_mem,$buffered_mem,$cached_mem)=$match[0];
$used_mem-=($buffered_mem+$cached_mem);
$percent_used=(int)(($used_mem*100)/$total_mem);
if($percent_used>$alert_percent)
exec("notify-send 'Low Memory: $percent_used% used'");
//sleep($interval);
//}
exit();
?>
free -m
是必须的。