exec()等待PHP中的响应 [英] exec() waiting for a response in PHP

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

问题描述

可能重复:
php exec命令(或类似命令)不等待结果

Possible Duplicate:
php exec command (or similar) to not wait for result

我有一个页面,该页面运行一系列exec()命令,这些命令强制我的PHP脚本停止更改,直到它收到响应为止.我该如何告诉exec()不要等待响应,而只是运行命令?

I have a page that runs a series of exec() commands which forces my PHP script to halt alteration until it receives a response. How can I tell exec() to not wait for a response and just run the command?

我正在使用一个具有后端系统的复杂命令,我可以查询该命令以检查状态,因此我不关心响应.

I'm using a complex command that has a backend system I can query to check the status, so I'm not concerned with a response.

推荐答案

取决于您使用的平台和运行的命令.

Depends on what platform you are using, and the command you are running.

例如,在Unix/Linux上,您可以在命令末尾附加> /dev/null &,以告诉Shell释放您已启动的进程,exec将立即返回.这在Windows上不起作用,但是有一种使用COM对象的替代方法(请参见下面的编辑).

For example, on Unix/Linux you can append > /dev/null & to the end of the command to tell the shell to release the process you have started and exec will return immediately. This doesn't work on Windows, but there is an alternative approach using the COM object (See edit below).

许多命令都有一个可以传递的命令行参数,因此它们释放与终端的关联并立即返回.另外,某些命令似乎挂起,因为它们已经问了一个问题,并且正在等待用户输入以告诉它们继续执行(例如,运行gzip且目标文件已存在时).在这种情况下,通常可以传递一个命令行参数来告诉程序如何处理而不提出问题(在gzip示例中,您将传递-f).

Many commands have a command line argument that can be passed so they release their association with the terminal and return immediately. Also, some commands will appear to hang because they have asked a question and are waiting for user input to tell them to continue (e.g. when running gzip and the target file already exists). In these cases, there is usually a command line argument that can be passed to tell the program how to handle this and not ask the question (in the gzip example you would pass -f).

编辑

只要COM可用,以下是在Windows上执行所需操作的代码:

Here is the code to do what you want on Windows, as long as COM is available:

$commandToExec = 'somecommand.exe';
$wshShell = new COM("WScript.Shell");
$wshShell->Run($commandToExec, 0, FALSE);

请注意,它是告诉WshShell启动程序然后立即返回的第三个FALSE参数(第二个0参数被定义为窗口样式",在这里可能没有意义-您可以传递任何整数值).在此处中记录了WshShell对象.绝对可以,我以前用过...

Note that it is the third, FALSE parameter that tells WshShell to launch the program then return immediately (the second 0 parameter is defined as 'window style' and is probably meaningless here - you could pass any integer value). The WshShell object is documented here. This definitely works, I have used it before...

我也在上面进行了编辑,以反映一个事实,即为了使&与* nix上的exec()配合使用,还需要使用管道连接到/dev/null.

I have also edited above to reflect the fact that piping to /dev/null is also required in order to get & to work with exec() on *nix.

还添加了有关WshShell的更多信息.

Also just added a bit more info about WshShell.

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

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