一、shell脚本 program.sh
#!/bin/sh
export LANG=zh_CN.GB2312
/bin/echo -e "start program!" >> /p2p.log
ps -ef|grep 'program' |grep -v grep |awk '{print $2}' |xargs -t -l kill -9
nohup /usr/java/jdk1.5.0_08/bin/java -jar /apps/program.jar 1>program.log&
启动时sh program.sh即可
二、看门狗shell脚本 monitor.sh
#!/bin/bash
#注:本脚本需要以超级用户身份运行。
# 监测的时间间隔,秒计
INTERVAL=60
# 重启时间间隔
INTERVALRESTART=1
#==================================================================
PROGRAME=program
THREADNUMS=1
LOG=/apps/monitor.log
#SYSDATE=$(date)
while true
do
#echo " "
#echo "**********Now begin to monitor program's status , please wait.************"
#echo " "
#`ulimit -c unlimited`
SYSDATE=$(date)
nowps1=`ps -e | grep $PROGRAME | grep -v grep | wc -l`
nowps1=`expr $nowps1`
#echo "ps = $nowps1"
if [ $nowps1 -lt $THREADNUMS ]; then
/usr/bin/killall -9 $PROGRAME 1>/dev/null 2>/dev/null
echo "======================================================" >> $LOG
echo "Restart date:" $SYSDATE >> $LOG
echo "Program name:" $PROGRAME >> $LOG
echo "======================================================" >> $LOG
#sleep $INTERVALRESTART
# ./$PROGRAME 1>ucfile 2>ucfile
/bin/sh /apps/server/p2pstart.sh
echo " "
echo "*******************************************************"
echo "---------- Program $PROGRAME restart ----------------"
echo "*******************************************************"
echo " "
fi
#echo "**********Now ended monitor program's status !*************************"
#echo " "
sleep $INTERVAL
done
#-----------------------------------------------------------
exit 0
如何在系统启动时启动看门狗程序:
将/bin/sh /apps/monitor.sh加入到文件/etc/rc.local中即可,这样每次系统启动都会运行该程序以监听自己要监听的程序
|