简体   繁体   English

PHP 等待 exec 完成并输出结果

[英]PHP wait for exec to finish and output the result

I have a php file that takes in a couple parameters from the URL and then runs an exec command, in which i want to wait and have the results of the exec displayed.我有一个 php 文件,它从 URL 接收几个参数,然后运行一个 exec 命令,我想在其中等待并显示 exec 的结果。 This exec command takes about 20-30 seconds to finish.此 exec 命令大约需要 20-30 秒才能完成。 It never completes because the webpage just gets an nginx 502 bad gateway error (times out).. Instead of extending nginx timeout error, as that's bad practice to have a connection hang for that long, how can i run the php's exec in the back and then have it returned on the page after it's complete?它永远不会完成,因为网页只是收到 nginx 502 错误网关错误(超时)。而不是扩展 nginx 超时错误,因为连接挂起这么长时间是不好的做法,我如何在后面运行 php 的 exec然后在完成后将其返回到页面上? Or is there a better way to accomplish this without using php?或者有没有更好的方法来完成这个而不使用 php?

Have the PHP trigger an exec to a script that is forked so it runs in the background (ends with & ).让 PHP 触发一个 exec 到一个分叉的脚本,以便它在后台运行(以&结尾)。 The page should then return some js that periodically polls the server via ajax requests to check the original script's status.然后页面应该返回一些 js,这些 js 定期通过 ajax 请求轮询服务器以检查原始脚本的状态。 The script should output its STDOUT and STDERR to unique file(s) so that the polling script can check the status.脚本应将其 STDOUT 和 STDERR 输出到唯一文件,以便轮询脚本可以检查状态。

Edit: If you need to know the script's exit code wrap it in another script:编辑:如果您需要知道脚本的退出代码,请将其包装在另一个脚本中:

#/bin/bash
uniqId=$1
yourscript $uniqId &
myPid=$!
echo $myPid > $uniqId'.pid'
wait $myPid
echo $? > $uniqId'.returned'

Call like ./wrapper.sh someIdUniqueToClient & ../wrapper.sh someIdUniqueToClient &一样调用。 Untested, but you get the gist.未经测试,但你明白了要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM