简体   繁体   English

通过exec()调用时,长时间运行的PHP脚本会停止,但在通过CLI调用时会结束

[英]Long running PHP script stops when called through exec(), but finishes when called through the CLI

I have a bunch of scripts which take a long time to complete. 我有一堆脚本需要很长时间才能完成。 Some of them can take up to 20 minutes. 其中一些可能需要20分钟。

There's a Bash script which executes these PHP scripts. 有一个执行这些PHP脚本的Bash脚本。 When I call this Bash script through the CLI as root, all the PHP scripts finish without any problems. 当我以root身份通过CLI调用此Bash脚本时,所有PHP脚本都会完成而没有任何问题。 But when I call the Bash script with PHP's exec() function through the browser, the scripts suddenly stop after 7/8 minutes without throwing any errors. 但是当我通过浏览器使用PHP的exec()函数调用Bash脚本时,脚本会在7/8分钟后突然停止而不会抛出任何错误。

Is there a certain restriction to the time a process or script can run when executed through Apache/PHP? 对通过Apache / PHP执行的进程或脚本运行的时间有一定的限制吗?

I've tried: 我试过了:

  • set_time_limit(0)
  • exec('nohup /path/to/bashscript')
  • exec('/path/to/bashscript | at now')

The last two tried solutions have been recommended by others who have had problems with long running scripts, but it doesn't help me at all. 最后两个尝试过的解决方案已被其他遇到长期运行脚本问题的人推荐,但它对我没有任何帮助。

Note: The Bash script which executes the PHP scripts is CakePHP's console app. 注意:执行PHP脚本的Bash脚本是CakePHP的控制台应用程序。 I have to execute the PHP scripts through this Bash script to make use of all the functionality of CakePHP (models, shell methods, etc). 我必须通过这个Bash脚本执行PHP脚本,以利用CakePHP的所有功能(模型,shell方法等)。 And I need to be able to call the Bash script through the browser, and let it run in the background. 我需要能够通过浏览器调用Bash脚本,并让它在后台运行。

The server is a VPS and has WHM/cPanel installed. 服务器是VPS并安装了WHM / cPanel。

you need to increase max execution time using (Careful while setting 0, it makes your execution time infinite) 你需要增加最大执行时间(设置为0时小心,它会使你的执行时间无限)

 ini_set('max_execution_time', 0);

but I would recommend to user 但我会向用户推荐

   proc_open();

over exec(); over exec(); this will pipe your processes and you can que other processes as well. 这将管道您的进程,您也可以排除其他进程。 Read more about proc_open [HERE] http://www.sitepoint.com/proc-open-communicate-with-the-outside-world/ ! 阅读更多关于proc_open [HERE] http://www.sitepoint.com/proc-open-communicate-with-the-outside-world/的信息

Set: 组:

ini_set('max_execution_time', 0);

on very top of your script. 在你的脚本的最顶层。 This will disable time limits. 这将禁用时间限制。 However, imo it does not make much sense to execute a 20 min script in the browser. 但是,在浏览器中执行20分钟的脚本没有多大意义。

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

相关问题 从exec()调用时,PHP脚本会部分失败 - PHP script fails part-way through when called from exec() TypeRocket:通过Controller调用函数时,php脚本停止运行 - TypeRocket: php script stops running when function is called via Controller 通过Apache调用MySQL时,PHP脚本在MySQL连接处中断 - PHP script breaks at MySQL connect when called through Apache 在PHP脚本中调用时,无法通过Perl建立MySQL连接 - Unable to make a MySQL connection through Perl when called in a PHP script 从浏览器调用PHP exec失败但从CLI调用失败 - PHP exec fails when called from a browser but not from CLI 'unoconv'脚本在终端中有效,但在我的laravel控制器中通过exec()函数调用时不起作用 - 'unoconv' script works in terminal but not when called through an exec() function in my laravel controller 通过nginx / php-fpm exec调用时npm不运行 - npm does not run when called through nginx/php-fpm exec 在localhost上从Java调用时,Php exec无法正常运行 - Php exec is not running properly when called from Java on localhost 使用 php shell_exec() 调用时搅拌机未运行 - Blender not running when called using php shell_exec() 从CLI调用时,PHP脚本何时停止执行? - When does a PHP script stop executing when called from CLI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM