程序监控脚本
guard_cmms.sh
#!/bin/bash
#需要守护的进程数组,将需要守护的进程填入数组中,如 PRO_NAMES=(./bin/pro1 ./pro2 ) 表示要守护pro1、pro2 进程
PRO_NAMES=(
"maincmms"
"haikangaccessdevice"
"dahuaaccessdevice"
)
#不保存控制台输出日志
OUTPUT="/dev/null"
#守护进程的数量
PRO_COUNT=${#PRO_NAMES[*]}
echo "PRO_COUNT = ${PRO_COUNT}"
start()
{
while true ; do
for ((i=0; i<${PRO_COUNT}; i++))
do
# 默认关闭日志输出
# OUTPUT="${PRO_NAMES[i]}.log"
# 用ps获取$PRO_NAME进程数量
NUM=`ps aux | grep ${PRO_NAMES[i]} | grep -v grep |wc -l`
echo -e "####${PRO_NAMES[i]} \t pro_count = $NUM"
# 少于1,重启进程
if [ "${NUM}" -lt "1" ];then
cd /opt/cvos/cmms
nohup ./${PRO_NAMES[i]} >${OUTPUT} 2>&1 &
cd -
# 大于1,杀掉所有进程,重启
elif [ "${NUM}" -gt "1" ];then
echo "##### more than 1 ${PRO_NAMES[i]},killall ${PRO_NAMES[i]}}"
killall ${PRO_NAMES[i]}
cd /opt/cvos/cmms
nohup ./${PRO_NAMES[i]} >${OUTPUT} 2>&1 &
cd -
fi
# kill僵尸进程
NUM_STAT=`ps aux | grep ${PRO_NAMES[i]} | grep T | grep -v grep | wc -l`
if [ "${NUM_STAT}" -gt "0" ];then
killall ${PRO_NAMES[i]}
cd /opt/cvos/cmms
nohup ./${PRO_NAMES[i]} >${OUTPUT} 2>&1 &
cd -
fi
done
sleep 2
done
}
stop()
{
for ((i=0; i<${PRO_COUNT}; i++))
do
killall ${PRO_NAMES[i]}
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring "
stop
echo "stop over!"
sleep 1
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
服务启停脚本
#!/bin/bash
#chkconfig: 2345 80 90
#description:guard_cmms.sh
start()
{
cd /opt/cvos/cmms/
nohup ./guard_cmms.sh start > /dev/null 2>&1 &
cd -
}
stop()
{
cd /opt/cvos/cmms/
killall guard_cmms.sh
./guard_cmms.sh stop
cd -
}
restart()
{
cd /opt/cvos/cmms
nohup ./guard_cmms.sh restart > /dev/null 2>&1 &
cd -
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
注意
编写程序作为守护监控,调用fork函数启动新进程,然后调用execl启动监控程序,发现主进程被阻塞