PHP 中的异步 shell exec [英] Asynchronous shell exec in PHP

查看:85
本文介绍了PHP 中的异步 shell exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 脚本,它需要调用一个 shell 脚本,但根本不关心输出.shell 脚本进行了许多 SOAP 调用并且完成速度很慢,所以我不想在等待回复时减慢 PHP 请求的速度.其实PHP请求应该可以在不终止shell进程的情况下退出.

I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should be able to exit without terminating the shell process.

我研究了各种 exec()shell_exec()pcntl_fork() 等函数,但没有一个他们似乎提供了我想要的东西.(或者,如果他们这样做,我不清楚如何.)有什么建议吗?

I've looked into the various exec(), shell_exec(), pcntl_fork(), etc. functions, but none of them seem to offer exactly what I want. (Or, if they do, it's not clear to me how.) Any suggestions?

推荐答案

如果它不关心输出",就不能用 & 调用脚本的 exec后台处理?

If it "doesn't care about the output", couldn't the exec to the script be called with the & to background the process?

EDIT - 结合@AdamTheHut 对这篇文章的评论,您可以将其添加到对 exec 的调用:

EDIT - incorporating what @AdamTheHut commented to this post, you can add this to a call to exec:

" > /dev/null 2>/dev/null &"

这会将stdio(第一个>)和stderr(2>)重定向到/dev/null 并在后台运行.

That will redirect both stdio (first >) and stderr (2>) to /dev/null and run in the background.

有其他方法可以做同样的事情,但这是最容易阅读的.

There are other ways to do the same thing, but this is the simplest to read.

上述双重重定向的替代方法:

An alternative to the above double-redirect:

" &> /dev/null &"

这篇关于PHP 中的异步 shell exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆