PHP监控进程并重启

此博客展示了一段PHP代码,用于监控指定目录下的多个PHP进程。通过`checkProcess`类的`check_php`方法检查进程是否运行,若未运行则调用`start_php`方法重启,并记录重启日志。代码会循环检查,每次间隔60秒。
<?php
// 监控进程并重启

ini_set('date.timezone','Asia/Shanghai');

class checkProcess{
   protected $dir = '';
   protected $deadtime = 0;
   public function __construct($dirname){
      $this->dir = $dirname;
   }
   //检查是否在执行
   public function check_php($filename){
      $fullname = $this->dir . "/" . $filename;
      // ps -ax|grep /mydata/data/mainscript/script_api/script_game_record.php|grep -v grep|wc -l
      $str = "ps -ef | grep " . $fullname . '|grep -v grep|wc -l'; //查看符合条件的进程数量,排除grep进程本身
      $tmp = exec($str);
      //不在运行
      if ( $tmp < 1 ){
         $this->start_php($fullname);
         // 写记录
         $logstr = "echo '[ ".date('Y-m-d H:i:s')." ] [ ".$filename." ] has been restarted ...' >> " . $this->dir . "/restart_log.log";
         exec($logstr);
         return true;
      }
   }
   public function start_php($fullname){
      $cmd = "nohup /usr/local/php/bin/php " . $fullname . ' > /dev/null 2>&1 &';
      exec($cmd);
   }
}
// ----------------------------------------------------------------------------------------------
$file_arr = [
   'script_coin_count.php',
   'script_game_record.php',
   'script_player_earn.php',
   'script_player_info.php',
   'script_player_reward.php',
   'script_rank.php',
   'script_channel_count.php',
   'script_clear_token.php',
   // 'script_jd_state_update.php',
   'script_channel_reward.php',
   'script_bonus_trace.php',
   'script_mail_queue.php',
   'script_mail_del.php',
   'script_partner_reward.php'
];
$dirname = '/mydata/data/mainscript/script_api';
$handller = new checkProcess($dirname);
while(true){
   if(!empty($file_arr)){
      foreach($file_arr as $val){
         $handller->check_php($val);
      }
   }
   sleep(60);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值