#!/bin/bash
phpBin=/usr/bin/php
#日志路径
logPath=/datasdc/logs/ysgame/log/
logFileName=migrateTableData_$(date +"%Y%m%d%H").log
logFile=${logPath}${logFileName}
#运行的php页面
phpFileName="/www/_www/ysgame/tools/guard/php/migrateTableData.php"
#shell路径需要和运行shell脚本的路径保持一致
shellFileName="migrateTableData.sh"
#写日志函数
function funcLog() {
  echo "$(date +"%Y-%m-%d %H:%M:%S") ${1} " >> ${logFile}
}



function funcStart() {
  funcLog "执行php代码"
  processCount=`ps aux|grep ${phpFileName}|grep -v "grep"|wc -l`
  funcLog "php脚本数量：$processCount"
  if [ "$processCount" -lt 1 ]; then
     ${phpBin} ${phpFileName} &
  else
    funcLog "有php代码在执行,不用重新运行php脚本，继续执行以前的php脚本"
  fi
}

funcLog "==启动同步数据库程序=="

funcStart

while true
do
  logFile=${logPath}${logFileName}
  funcLog "检测进程状态"
  processCount=`ps aux|grep ${phpFileName}|grep -v "grep"|wc -l`
  processid=`ps aux|grep ${phpFileName}|grep -v "grep"|awk '{print $2}'`
  #进程大于1
  if [ "$processCount" -gt 1 ]; then
    funcLog "检测进程大于1个，结束进程ID：$processid"
    kill -9 $processid
  fi
  #进程小于1
  if [ "$processCount" -lt 1 ]; then
    funcLog "检测进程小于1个，重新启动php执行"
    funcStart
  fi
  funcLog "获取当前PHP进程ID：${processid}"
  sleep 10
done