不幸的是,我发现所有用于执行外部程序的解决方案都不适合,因此我使用了自己的实现,即pcntl_execafter pcntl_fork。
但是现在我需要将已执行程序的stderr / stdout重定向到某个文件中。显然,我应该在之后使用某种dup2Linux调用pcntl_fork,但是dup2我在PHP中只能看到eio_dup2,它看起来像不是在运行常规流(例如stderr / stdout),而是在运行一些异步流。
如何dup2从PHP 调用或如何在没有它的情况下重定向std *?
相同的问题(尽管没有详细信息)没有答案:如何从PHP调用dup2()syscall?
这里出现了一种不需要 的方法dup2。正是基于这个答案。
$pid = pcntl_fork();
switch($pid) {
case 0:
// Standard streams (stdin, stdout, stderr) are inherited from
// parent to child process. We need to close and re-open stdout
// before calling pcntl_exec()
// Close STDOUT
fclose(STDOUT);
// Open a new file descriptor. It will be stdout since
// stdout has been closed before and 1 is the lowest free
// file descriptor
$new_stdout = fopen("test.out", "w");
// Now exec the child. It's output goes to test.out
pcntl_exec('/bin/ls');
// If `pcntl_exec()` succeeds we should not enter this line. However,
// since we have omitted error checking (see below) it is a good idea
// to keep the break statement
break;
case -1:
echo "error:fork()\n";
exit(1);
default:
echo "Started child $pid\n";
}
Run Code Online (Sandbox Code Playgroud)
为了简洁起见,省略了错误处理。但请记住,在系统编程中应该小心处理任何函数的返回值。