PHP:如何启动独立进程? [英] PHP: how to start a detached process?
本文介绍了PHP:如何启动独立进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前我的解决方法是:
exec('php file.php >/dev/null 2>&1 &');
和在file.php中
and in file.php
if (posix_getpid() != posix_getsid(getmypid()))
posix_setsid();
有什么我可以通过exec来做到这一点的方法吗?
is there any way I can do this just with exec?
推荐答案
没有,exec()
(也不能是shell_exec()
或system()
)不能做到这一点
No this can't be done with exec()
(nor shell_exec()
or system()
)
如果您安装了 pcntl扩展名,则它将是: /p>
If you have the pcntl extension installed it would be:
function detached_exec($cmd) {
$pid = pcntl_fork();
switch($pid) {
// fork errror
case -1 : return false
// this code runs in child process
case 0 :
// obtain a new process group
posix_setsid();
// exec the command
exec($cmd);
break;
// return the child pid in father
default:
return $pid;
}
}
这样称呼:
$pid = detached_exec($cmd);
if($pid === FALSE) {
echo 'exec failed';
}
// do some work
// kill child
posix_kill($pid, SIGINT);
waitpid($pid, $status);
echo 'Child exited with ' . $status;
这篇关于PHP:如何启动独立进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文