问题描述
我运行其中涉及与PHP + Apache2的守护程序的Web服务。所以,我想pcntl_fork功能。但是有一个问题,子进程不会终止,即使我用的子进程的code出口(0),这导致了大量的Apache2流程。
I am running a web service which involved with daemons by php+apache2. So I tried pcntl_fork function. But there is a question that the child process are not terminating even I used exit(0) in the child process's code which result in a lot of apache2 processes.
我不知道是否有一种方法来关闭那些无用的Apache2进程?
I'm wondering if there is a way to shutdown those useless apache2 processes?
PS:因为我不是很清楚信号的机制,所以我想通过一个代理脚本将尽快向孩子产生退出的单一调用进行守护。
PS: because I'm not very aware of the mechanism of signal, so I tried to make daemon by a single call to a agent script which will exit as soon as the child is created.
switch ($_GET['action']){
case "new":
$pid = pcntl_fork();
switch ($pid){
case -1:
echo "failed to create daemon";
exit;
case 0:
//Code here
exit(0);
break;
default:
echo "Daemon PID:$pid";
}
}
和我计划用一个文件来控制守护进程。例如,我会追加像退出的守护进程的控制文件,如1.txt的一条线,让它关机本身。
And I'm planning to use a file to control the daemon. For example I will append a line like "exit" to the daemon's control file such as "1.txt" to let it shutdown itself.
PPS:阅读这个主题之后:pcntl_fork()导致倒闭的父进程,我很好奇,如果僵尸进程的错误导致了错误。
PPS: After reading this topic: pcntl_fork() results in defunct parent process, I'm curious about that if the zombie process bug caused the bug.
推荐答案
您应该使用此功能:
但一般在Apache的分叉可能不是一个好主意。
But generally under Apache forking is probably not a good idea.
这篇关于从pcntl_fork过程没有结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!