CakeFest 2024: The Official CakePHP Conference

基本用法

这个示例用于产生一个守护进程并可以通过信号处理进行关闭。

示例 #1 进程控制示例

<?php
declare(ticks=1);

$pid = pcntl_fork();
if (
$pid == -1) {
die(
"could not fork");
} else if (
$pid) {
exit();
// 父进程
} else {
// 子进程
}

// 从控制终端分离
if (posix_setsid() == -1) {
die(
"could not detach from terminal");
}

// 安装信号处理程序
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");

// 执行无限循环任务
while (1) {

// do something interesting here

}

function
sig_handler($signo)
{

switch (
$signo) {
case
SIGTERM:
// 处理终止任务
exit;
break;
case
SIGHUP:
// 处理重启任务
break;
default:
// 处理所有其它信号
}

}

?>
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top