在Linux下我们可以会开启一些后台的php进程来处理一些任务,因此我们需要来管理这些进程,启动,关闭,重启。
方法1:写一个php程序来监控他们,染乎将这个程序后台运行
check_process.php
<?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 php " . $fullname . ' > /dev/null 2>&1 &';
exec($cmd);
}
}
// ----------------------------------------------------------------------------------------------
$file_arr = [
'script_coin_count.php',
'script_game_record.php',
'script_activity.php',
'script_other.php',
'script_rewite_game_record.php',
];
$dirname = '/mydata/data//async_script';
$handller = new checkProcess($dirname);
while(true){
if(!empty($file_arr)){
foreach($file_arr as $val){
$handller->check_php($val);
}
}
sleep(60);
}
方法2:使用Supervisor程序
Supervisor 是一个 Linux 下的进程管理器,需要安装
文档:http://supervisord.org/index.html