monitor.sh监控进程并重启拉起

 

比如在服务器开启了php死循环程序,为了防止php进程僵死或挂掉,需要sh监控进程并重启拉起。

当日志记录时间超过5分钟就kill重启拉起

 

 

 

 

#!/bin/bash
#
# 请使用crontab -e 来输入命令每隔一定时间调用本程序
# */1 * * * * /bin/sh /var/www/test
/protected/commands/shell/monitor.sh
#
#日志文件
LOG_DATE=`date +%Y-%m-%d`
LOG_PATH="/var/www/logs/monitor_${LOG_DATE}.log"
run_php_path="/usr/local/php/bin/php"
 if [[ $0 = \* ]] then curfile="$0" else curfile="$PWD/${0#./}" fi php_path=`dirname $curfile` #yiicmd.php所在目录 phpCmdDir=`dirname $php_path`


#yii命令行都是执行此php
phpCmdName="yiicmd.php"


# 配置监控的程序及对应的监控频率时间及允许启动的线程数
shconfig[0]="drawresult" shtime[0]=600 shconfig[1]="kkdrawresult" shtime[1]=600





LOG_START_TIME=`date +'%Y-%m-%d %H:%M:%S'`
echo " " >> $LOG_PATH 
echo "monitor.sh run start...... time : $LOG_START_TIME" >> $LOG_PATH 


#下面这里不用改路径
IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`

#当前进程数
MAX_THREAD=4
CUR_THREAD=0
CUR_SERVER=0

# 循环取配置的各个进程一个个检查并关闭
i=0
for index in "${shconfig[@]}"
do

    echo "check ${phpCmdDir} ${phpCmdName} ${index} thread......" >> $LOG_PATH 
    #线程数
    # 判断是不是有重复进程,保证一台电脑上只有一个进程
	counter=`ps -ef | grep "${phpCmdDir}${phpCmdName} ${index}" | grep -v grep | wc -l`
	if [ $counter -gt 1 ]; then
		echo "kill mulithreed ${phpCmdDir}${phpCmdName} ${index}" >> $LOG_PATH 
		ps -ef | grep "${phpCmdDir}${phpCmdName} ${index}" | grep -v "grep" | awk '{print $2}'|xargs kill
		sleep 1
	fi
	
	#检查进程 拉起进程


	if [ $counter -eq 0 ]; then
		echo "run ${phpCmdDir}${phpCmdName} ${index}

" >> $LOG_PATH 
		${run_php_path} ${phpCmdDir}${phpCmdName} ${index} &
		continue 1
	fi

    last_time=`date +%s`
    FILENAME="/var/www/logs/${index}${CUR_THREAD}.time"
    echo  "${FILENAME}" >>$LOG_PATH 
    if [ -e $FILENAME ]
    then
        last_time="`cat ${FILENAME}`"
    else
        last_time=0
    fi

    if [ -z $last_time ]
    then
      last_time=0
    fi
    current_time=`date +%s`
    let "diff_time = current_time - $last_time "
    echo "${current_time} ${last_time} = ${diff_time} > ${shtime[i]}" >> $LOG_PATH

    # 如果时间超过50秒,重启
    if [ "$diff_time" -ge "${shtime[i]}" ]
       then

        # 关闭此进程    
        echo "stop $index......" >> $LOG_PATH 
        chpid="`cat /var/www/logs/${index}.sid`";
        kill $chpid;
		
        echo "kill ${phpCmdDir}${phpCmdName} ${index} done!" >> $LOG_PATH 
        ps ax|grep "${phpCmdDir}${phpCmdName} ${index}" |grep -v grep|awk '{print $1}'|xargs kill
		
        # 休眠两秒后再启动服务
        sleep 2
        echo "restart ${phpCmdDir}${phpCmdName} ${index}......" >> $LOG_PATH 
        echo "${run_php_path} ${phpCmdDir}${phpCmdName} ${index}" >> $LOG_PATH 
        
        ${run_php_path} ${phpCmdDir}${phpCmdName} ${index} &
        sleep 1
        
        shpid="$!";
        echo "$shpid" > /var/www/logs/${index}.sid		
        echo "success restart $index......" >> $LOG_PATH 
    fi
    i=$(($i+1))
done
LOG_END_TIME=`date +'%Y-%m-%d %H:%M:%S'`
echo "monitor.sh run end......time: $LOG_END_TIME" >> $LOG_PATH 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Monitor)