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

时间:2022-10-20 15:48:12

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. When I call this Bash script through the CLI as root, all the PHP scripts finish without any problems. 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脚本的Bash脚本。当我以root身份通过CLI调用此Bash脚本时,所有PHP脚本都会完成而没有任何问题。但是当我通过浏览器使用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')
  • 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. I have to execute the PHP scripts through this Bash script to make use of all the functionality of CakePHP (models, shell methods, etc). And I need to be able to call the Bash script through the browser, and let it run in the background.

注意:执行PHP脚本的Bash脚本是CakePHP的控制台应用程序。我必须通过这个Bash脚本执行PHP脚本,以利用CakePHP的所有功能(模型,shell方法等)。我需要能够通过浏览器调用Bash脚本,并让它在后台运行。

The server is a VPS and has WHM/cPanel installed.

服务器是VPS并安装了WHM / cPanel。

2 个解决方案

#1


1  

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(); 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/!

over exec();这将管道你的进程,你也可以排队其他进程。阅读更多关于proc_open的信息[这里] http://www.sitepoint.com/proc-open-communicate-with-the-outside-world/!

#2


0  

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分钟的脚本没有多大意义。

#1


1  

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(); 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/!

over exec();这将管道你的进程,你也可以排队其他进程。阅读更多关于proc_open的信息[这里] http://www.sitepoint.com/proc-open-communicate-with-the-outside-world/!

#2


0  

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分钟的脚本没有多大意义。