#Program:
#This is a program that can monitor the Program is activity and how many resource it occupy.
#History:
# 2012-02-27 Daniel.W|Kernel First Release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
port=""
#declare -i max_cpu
#declare -i max_mem
#declare -i cpu
#declare -i men
function com(){
if [ -n "$port" ];then
result=$(ps aux | grep -w $pro | grep port=$port | grep -v grep)
else
result=$(ps aux | grep -w $pro | grep -v grep)
fi
}
read -p "Which path the program is:" path
read -p "The mail address do you want ro send:" send_mailbox
read -p "The mail address do you want to receive the warning message:" mailbox
read -p "The maximum CPU occupancy rate:" max_cpu
read -p "The maximum memory occupancy rate:" max_mem
read -p "How many seconds do you want to run:" time
read -p "Which port do you want to monitor(#not necessary,only for mult port process):" port
pro=$( echo $path | awk 'BEGIN{FS="/"}{print $NF}' )
while [ 1 ]
do
com
sleep $time
echo $result
if [ -z "$result" ];then
echo "The $pro process has down,now i try to restart it" | mail -s "Program Monitor Warning[Err:1]" $mailbox
$path &
sleep 20
fi
cpu=$( ps aux | grep -w $pro | grep -v grep | awk '{print $3}' )
mem=$( ps aux | grep -w $pro | grep -v grep | awk '{print $4}' )
echo "CPU:"$cpu
echo "MEM:"$mem
#if_over_cpu="no"
#if_over_mem="no"
echo $cpu
echo $max_cpu
if_over_cpu=$(echo $cpu:$max_cpu | awk 'BEGIN{FS=":"}{if($1>=$2) {print "yes"} else {print "no"}}')
if_over_mem=$(echo $mem:$max_mem | awk 'BEGIN{FS=":"}{if($1>=$2) {print "yes"} else {print "no"}}')
echo $if_over_cpu
echo $if_over_mem
if [ "$if_over_cpu" == "yes" ];then
echo "2"
echo "The $pro process has pass the maximum set for CPU occupancy" | mail -s "Program Monitor Warning[Err:2]" $mailb
ox -- -f$send_mailbox
fi
if [ "$if_over_mem" == "yes" ];then
echo "3"
echo "The $pro process has pass the maximum set for Memory occupancy" | mail -s "Program Monitor Warning[Err:3]" $ma
ilbox -- -f$send_mailbox
fi
done
脚本没有对输入的数据进行判断,大家可以加以改进