#!/bin/bash
PRO_NAME=test
cd /home/wwwroot/default
while true ; do
#用ps获取$PRO_NAME进程数量
NUM=`ps aux | grep -w "/usr/bin/php think ${PRO_NAME}" | grep -v grep |wc -l`
#echo $NUM
#少于1,重启进程
if [ "${NUM}" -lt "1" ];then
echo "${PRO_NAME} was killed"
nohup /usr/bin/php think ${PRO_NAME} >> ${PRO_NAME}.log 2>&1 &
#大于1,杀掉所有进程,重启
elif [ "${NUM}" -gt "1" ];then
echo "more than 1 ${PRO_NAME},killall ${PRO_NAME}"
ps aux | grep -w ${PRO_NAME} | grep -v grep |grep -v ${PRO_NAME}_deamon.sh |awk '{print $2}' |xargs kill -9
nohup /usr/bin/php think ${PRO_NAME} >> ${PRO_NAME}.log 2>&1 &
fi
sleep 5s
done
exit 0