#!/bin/sh
function plive() #一個參數,傳遞過濾的進程名,
{
num1=`ps aux |grep $1 |grep -v 'grep'|wc -l` #num=0,$1 is killed
echo $num1
}
{
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
}
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
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
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
sleep 2 #每隔兩秒檢測一次
done
done