#!/bin/sh
function plive() #一个参数,传递过滤的进程名,
{
        num1=`ps aux |grep $1 |grep -v 'grep'|wc -l` #num=0,$1 is killed
        echo $num1
}
function getpid(){ #一个参数,传递监控的进程名,得到期pid
        num2=`ps aux |grep $1|grep -v 'grep'|awk '{print $2}'`
        echo $num2
}
 
 
hostip=`ifconfig eth0 |grep "inet addr"|awk '{print $2}'|awk -F ':' '{print $2}'` #得到本机ip
date=`date +"%Y-%m-%d %H:%M:%S"`
while true
do
num3=0
num3=`plive xxserver`
echo $num3
if [ $num3 = 0 ]
then
        echo $date > ./monitor_nn.log
        echo "$hostip xxserver already stop" >> ./monitor_nn.log
        echo "$hostip xxserver starting..." >> ./monitor_nn.log
        nohup /root/code/xxserver & 
        pid=`getpid nnserver`
        if [ $pid -ne 0 ]
        then
                echo $date >> ./monitor_nn.log
                echo "$hostip nnserver already start" >> ./monitor_nn.log
                echo "$hostip nnserver24's pid is $pid" >> ./monitor_nn.log
        else
                echo $date >> ./monitor_nn.log
                echo "$hostip nnserver start failed" >> ./monitor_nn.log
        fi
else
        mail -s 'monitor_nn_status' [email protected] <./monitor_nn.log #启动不成功发送邮件
fi
sleep 2  #每隔两秒检测一次
done