yii2.0 console执行php守护进程

//该方法只需执行一次
public function actionIndex(){


$pid =pcntl_fork();//在当前进程中生成一个新的子进程
//$pid会有三种形式 $pid==-1生成子进程失败 $pid==0生成子进程成功 $pid>0在父进程中
if($pid == -1) {
return false;
} elseif($pid > 0) {
return false;
}

posix_setsid();//将当前进程选举成为进程组的leader
$pid = pcntl_fork();//为什么要重新运行pcntl_fork(子进程脱离父进程后会有系统托管,所以需要重新执行pcntl_fork)

if($pid == -1) {
return false;
} elseif($pid > 0) {
return false;
}


chdir("/");
umask(0);
fclose(STDIN);//关闭所有打开的文件指针
fclose(STDOUT);//
fclose(STDERR);

$i = 0;

while(1) {
$i++;
if($i >= 9) {
Yii::$app->db->createCommand('select 1')->execute();
$i = 0;
}
$this->a=$this->a+1;

echo $this->a;

file_put_contents("test.log",$this->a,FILE_APPEND);

sleep(3);
}


}
原文地址:https://www.cnblogs.com/paulversion/p/7170089.html