鍍金池/ 問答/PHP  Linux/ linux上的PHP腳本為什么掛掉了進程還在?

linux上的PHP腳本為什么掛掉了進程還在?

本人之前在linux上寫的一段PHP代碼(邏輯是用PHP實現(xiàn)對數(shù)據(jù)庫中日志文件的轉(zhuǎn)移,運行是shell腳本運行),為什么掛掉后(不能正常轉(zhuǎn)移數(shù)據(jù)庫中的數(shù)據(jù)),進程卻還在,根本就看不出來他掛掉。具體的PHP代碼如下:

/**
 * 測試
 */
class PaizuTableHandler extends ScriptBase
{
    public static $mysql = null;
    const LOG_PAIZU_TABLE = 'log_user_paiju';
    public function logic()
    {
        if(empty(self::$mysql)){
            self::$mysql = $this->mysql('logs');
        }
        set_time_limit(0);
        while (true)
        {
            $date = date("Y-m-d");
            $dataTime =  strtotime($date);
            $timestampStart = $dataTime - 3600*24; //昨天
            $timestampEnd = mktime(0,0,0,date("m"),1,date("Y"));//本月月初 例如:2017-07-01 00:00:00
            $timestampEnd_lastmoth = mktime(0, 0 , 0,date("m")-1,1,date("Y"));//上月月初 例如:2017-06-01 00:00:00

            if($timestampStart < $timestampEnd){
                sleep(60);
                continue;
            }

            $table = self::LOG_PAIZU_TABLE.'_'.date("Ym");//本月的表
            $lastMothTable = self::LOG_PAIZU_TABLE.'_'.date("Ym",mktime(0, 0 , 0,date("m")-1,1,date("Y")));//上個月的表

            $hasTable = self::$mysql->tableExists($table);  //判斷是否有本月的表
            $hasTableLast = self::$mysql->tableExists($lastMothTable);  //判斷是否有上個月的表

            $createFlag = $hasTable ? true : false; //本月的表是否存在
            $createFlagLast = $hasTableLast ? true : false; //上個月的表是否存在

            if( empty( $createFlag ) ){
                //創(chuàng)建表
                $createSql = "CREATE TABLE {$table} LIKE log_user_paiju";
                $createFlag = self::$mysql->query($createSql);
            }
            if( $createFlag ){

                //查詢本月當天應(yīng)清空數(shù)據(jù)有沒有清空
                $sql = "select * from log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd} and {$timestampStart} order by `time` desc limit 100";
                $result = self::$mysql->query($sql);
                $rs = self::$mysql -> fetch_row($result);
                if( !$result ){
                    break;
                    exit(0);
                }

                $date = date("Y-m-d");
                $current_time =  strtotime($date); //當前時間戳
                $current_time2 = mktime(0,0,0,date("m"),6,date("Y")); //月初第6天
                $last_rs = array(); //添加于2017.10.25
                if($current_time == $current_time2 && !empty( $createFlagLast ) && empty($rs)){
                    //本月第6天把 上個月的數(shù)據(jù)全部放到上個月的表中(要放最少2天前)
                    $last_sql = "select * from log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd_lastmoth} and {$timestampEnd} order by `time` desc limit 100";
                    $last_sql1 = "INSERT INTO {$lastMothTable}(uid,paiju_score,paizu_score,`time`,paizu_id,seq,room_id,server_id,creator,`status`,mahjongType) SELECT uid,paiju_score,paizu_score,`time`,paizu_id,seq,room_id,server_id,creator,`status`,mahjongType FROM log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd_lastmoth} and {$timestampEnd} order by `time` desc limit 100";
                    $last_sql2 = "delete from log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd_lastmoth} and {$timestampEnd} order by `time` desc limit 100";

                    $result1 = self::$mysql->query($last_sql);
                    $last_rs = self::$mysql -> fetch_row($result1);
                    if($last_rs){
                        if($r = self::$mysql->query($last_sql1)){
                            self::$mysql->query($last_sql2);
                        }
                    }


                }

                $sql1 = "INSERT INTO {$table}(uid,paiju_score,paizu_score,`time`,paizu_id,seq,room_id,server_id,creator,`status`,mahjongType) SELECT uid,paiju_score,paizu_score,`time`,paizu_id,seq,room_id,server_id,creator,`status`,mahjongType FROM log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd} and {$timestampStart} order by `time` desc limit 100";
                $sql2 = "delete from log_user_paiju where UNIX_TIMESTAMP(`time`) between {$timestampEnd} and {$timestampStart} order by `time` desc limit 100";

                if(empty($rs) && empty($last_rs)){
                    sleep(60);
                }
                if($rs){
                    if($r1=self::$mysql->query($sql1)){
                        !self::$mysql->query($sql2);
                    }
                }
            }
            usleep(3000);
        }
    }

}

====================================分割線================================

shell腳本如下:

#!/bin/sh
# scripts daemon, add to /etc/crontab
# */1 *   *   *   *   root    PATH/script.sh --daemon

# run on root
if [ "$(uname -s|grep -F 'CYGWIN')" = "" ] && [ $UID -ne 0 ]; then
    echo "superuser privileges are required to run this script."
    exit 1
fi

# settings
PHP_CLI="/usr/bin/php -c /etc/php.ini"
SCRIPTS="PaizuTableHandler"

# paths
BASE_PATH=$(cd $(dirname $0) && pwd)
BASE_SCRIPT="$BASE_PATH/script.php"
LOGS_PATH="$BASE_PATH/data/logs/bash.log"

USAGE="Usage: $(basename $0) [-d|-a|-k SCRIPT]\n\
  -d, --daemon\t\tthe daemon mode, runing checking\n\
  -a, --killall\t\tkill the all scripts\n\
  -k, --kill SCRIPT\tkill the SCRIPT\n\
  -h, --help\t\tthis help text"

function get_pid()
{
  echo $(ps -ef|grep -F "$1"|grep -v "grep"|awk '{print $2}')
}

function kill_script()
{
  PID=$(get_pid "$1")
  if [ "$PID" = "" ]; then
    echo "$1 not found"
  else
    kill -9 $PID
    echo "$1 has been killed, pid: $PID"
  fi
}

case $1 in
  -k|--kill)
    if [ "$2" = "" ]; then
      echo -e $USAGE
      exit 0
    fi
    kill_script "$BASE_SCRIPT request/$2"
    ;;
  -a|--killall)
    for SCRIPT in $SCRIPTS; do
      kill_script "$BASE_SCRIPT request/$SCRIPT"
    done
    ;;
  -d|--daemon)
    for SCRIPT in $SCRIPTS; do
      SCRIPT="$BASE_SCRIPT request/$SCRIPT"
      PID=$(get_pid "$SCRIPT")
      if [ "$PID" = "" ]; then
        eval "$PHP_CLI $SCRIPT >/dev/null 2>&1 &"
        PID=$(get_pid "$SCRIPT")
        echo "[$(date +%Y-%m-%d\ %H:%M:%S)] $SCRIPT has been down, restarted: $PID" >> $LOGS_PATH
      fi
    done
    ;;
  *)
    echo -e $USAGE
    ;;
esac
回答
編輯回答
執(zhí)念

crontab 一分鐘執(zhí)行一次shell,shell case 倒數(shù)第一個,然后你把標準輸出標準錯誤重定向了/dev/null,話說,掛掉后(不能正常轉(zhuǎn)移數(shù)據(jù)庫中的數(shù)據(jù))的處理邏輯在哪里呢?PHP拋異常不算掛掉哈,你的shell會繼續(xù)喚起的

2017年4月2日 09:11