用 PHP 的 shell_exec 调用 bash -- 慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling bash with PHP's shell_exec -- slow
提问by Tal
I'm using PHP's shell_exec function to call a bash script on my server.
我正在使用 PHP 的 shell_exec 函数在我的服务器上调用 bash 脚本。
shell_exec("bash -x /tesladata/isetools/0-extractbytickerforweb.bash $ticker $isedate > /t24alv2/iseoutput/$ticker-$isedate-$thistime.log &");
Now, I previously had the command running from a CGI script ("bash -x...") and it was much faster (instantaneous). Now it takes a painfully slow time for the script to run (> 10sec) and for the resulting page to render.
现在,我以前从 CGI 脚本(“bash -x...”)运行命令,而且速度要快得多(即时)。现在,脚本运行(> 10 秒)和呈现结果页面需要非常缓慢的时间。
Any ideas why this is so slow? I'd still like to run the bash script from PHP and not CGI, since my entire site is being converted to PHP. Perhaps another function is more suitable? Any ideas would be appreciated.
任何想法为什么这这么慢?我仍然想从 PHP 而不是 CGI 运行 bash 脚本,因为我的整个站点都被转换为 PHP。也许另一个功能更合适?任何想法,将不胜感激。
回答by tplaner
You can take a look at PHP's execand systemfunctions, however I don't really see a reason why they would speed up the execution of the script, worth a try though. I'm pretty sure it is an issue with apache (assuming you're using apache), not PHP the source of this conclusion being this bug thread.
你可以看看 PHP 的exec和system函数,但是我真的看不出它们会加速脚本执行的原因,不过值得一试。我很确定这是 apache 的问题(假设您使用的是 apache),而不是 PHP 这个结论的来源是这个 bug thread。
Also you really should be extremely careful of using these commands on a public website. Make use of escapeshellargand escapeshellcmd.
此外,您真的应该非常小心在公共网站上使用这些命令。使用escapeshellarg和escapeshellcmd。
回答by Clint Harris
For Windows users running Apache as an NT Service: It seems that you can greatly improve the shell_exec() or exec() performance by configuring the Apache service to run with a user accountand not the default system account.
对于将 Apache 作为 NT 服务运行的 Windows 用户:通过将 Apache 服务配置为使用用户帐户而不是默认系统帐户运行,您似乎可以大大提高 shell_exec() 或 exec() 性能。
For example, I found that running Apache as a standard NT Service resulted in shell_exec() commands taking 15-17 seconds (specifically, running svn commands). Changing the Apache service to run with a user account caused the time to drop to to 4-5 seconds--a huge difference.
例如,我发现将 Apache 作为标准 NT 服务运行会导致 shell_exec() 命令花费 15-17 秒(特别是运行 svn 命令)。更改 Apache 服务以使用用户帐户运行导致时间下降到 4-5 秒——一个巨大的差异。
To do this, open the service control panel, right-click on the Apache service, and select Properties. Click on the Log On tab and change the "Local system account" radio button to "This account". Then specify which user account you want the service to use.
为此,请打开服务控制面板,右键单击 Apache 服务,然后选择属性。单击登录选项卡并将“本地系统帐户”单选按钮更改为“此帐户”。然后指定您希望该服务使用哪个用户帐户。
Note that I'm not a Windows admin guru; running a service with a user account may have important implications that I'm not aware of.
请注意,我不是 Windows 管理员;使用用户帐户运行服务可能会产生我不知道的重要影响。

