我有一个PHP脚本,需要调用shell脚本,但根本不关心输出。shell脚本执行了许多SOAP调用,完成起来很慢,因此我不想在PHP请求等待应答时降低它的速度。事实上,PHP请求应该能够在不终止shell进程的情况下退出。
我已经研究了各种exec()、shell_exec()、pcntl_fork()等函数,但它们似乎都不能提供我想要的东西。(或者,即使他们这样做,我也不清楚是如何做到的。)有什么建议吗?
我有一个PHP脚本,需要调用shell脚本,但根本不关心输出。shell脚本执行了许多SOAP调用,完成起来很慢,因此我不想在PHP请求等待应答时降低它的速度。事实上,PHP请求应该能够在不终止shell进程的情况下退出。
我已经研究了各种exec()、shell_exec()、pcntl_fork()等函数,但它们似乎都不能提供我想要的东西。(或者,即使他们这样做,我也不清楚是如何做到的。)有什么建议吗?
当前回答
Php-execute-a-background-process有一些很好的建议。我觉得我的很好,但我有偏见:)
其他回答
使用命名fifo。
#!/bin/sh
mkfifo trigger
while true; do
read < trigger
long_running_task
done
然后,每当您想启动长时间运行的任务时,只需向触发器文件写入一个换行符(非阻塞)。
只要您的输入小于PIPE_BUF,并且它是一个单独的write()操作,您就可以将参数写入fifo并在脚本中显示为$REPLY。
如果它“不关心输出”,脚本的exec不能被调用和后台进程?
编辑-结合@AdamTheHut对这篇文章的评论,你可以把它添加到对exec的调用中:
" > /dev/null 2>/dev/null &"
这将把stdio (first >)和stderr(2>)重定向到/dev/null并在后台运行。
做同样的事情还有其他方法,但这是最简单的。
上述双重定向的替代方法:
" &> /dev/null &"
如果没有使用队列,你可以像这样使用proc_open():
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w") //here curaengine log all the info into stderror
);
$command = 'ping stackoverflow.com';
$process = proc_open($command, $descriptorspec, $pipes);
对所有Windows用户:我发现了一个运行异步PHP脚本的好方法(实际上它几乎适用于所有东西)。
它基于popen()和pclose()命令。并且在Windows和Unix上都能很好地工作。
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
原始代码来自:http://php.net/manual/en/function.exec.php#86329
我还发现Symfony Process Component在这方面很有用。
use Symfony\Component\Process\Process;
$process = new Process('ls -lsa');
// ... run process in background
$process->start();
// ... do other things
// ... if you need to wait
$process->wait();
// ... do things after the process has finished
在GitHub回购中查看它是如何工作的。