0

first of all: sorry if this has already been answered. I made multiple searches and tests, but couldn't find a real answer to my question (some answers but for <7.0.0).

Issue: I would like to increase the max execution time for a given script (let say 5 minutes), and this script only. I want my other scripts to keep using the .ini max execution time (30 seconds).

My .ini has a max exec time at 30 seconds. In the begining of the said script, I tried:

set_time_limit(0);
ini_set('max_execution_time', 0);

But it seems to have no effect, my script always timeout after 30s (.ini value). From what I could read on SO, those methods only work if "PHP Safe mode is disabled". But, from the PHP website :

This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

From SO:

This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.

So my questions:

  • does set_time_limit and ini_set('max_execution_time', 0) should work without additionnal .ini config since PHP 5.4.0 ? (and so, on PHP 7.4.8)
  • I don't want to set 5 minutes in my .ini, so what is the real way to make only 1 script have a longer max execution time?
  • Now that there is no more safe mode, those methods should work anytime?

Thanks a lot

Mtxz
  • 3,749
  • 15
  • 29
  • it you are using php-fmp, there is a setting in the apache/nginx configuration that sets a timeout for how long apache/nginx waits before it timeout. to know if that timeout is the issue run a loop that writes a log to a file and if you are getting a timeout in the browser while the log file is still beeing written to then your issue is apache/nginx not php – Nathanael Aug 11 '20 at 12:39
  • @Mtxz I'm using php7.3 apache and also have a script with at beggining "ini_set('max_execution_time', 0);" for unlimited time and it works. I never tested in php 7.4. Are you using FPM or something else ? – BackTrack57 Aug 11 '20 at 12:40
  • If you read the doc, `set_time_limit();` resets the `max_execution_time`, so try placing this command in you main control loop so it gets reset multiple times as in `set_time_limit(60);` or to set the timeout to a large number use `set_time_limit(6000);` at the top of your script – RiggsFolly Aug 11 '20 at 12:51
  • @Nathanael yes I'm using fpm with the latest Nginx. I'll take a look at my Nginx config to see, thanks for the tip. BackTrack57 I guess 7.4 FPM should handle this the same way as 7.3 does. As Nathanael mentioned, I may find something on the Nginx side. RiggsFolly from the doc, using "0" value should make unlimited execution time. Thanks you all, I'll report back after more tests. – Mtxz Aug 11 '20 at 13:01

1 Answers1

0

One option is to use a background process. Basically you create some form a queue that your calling script writes to and then you run a cron job that reads from that queue and the cron job runs yous script.

The php timeout only applies to scripts ran from your http server and does not apply to scripts ran from batch/cron so your cron job can run forever independently of what you have set in your php.ini.

Nathanael
  • 870
  • 5
  • 11
  • I get your point, but I'm not on a framework (laravel like) that already implements a queue system. My script is synchronizing a lot of objects from a remote API (50k), and each object needs 5 to 10s execution time on my server-side. Indeed a queue system would work, but is out of my client budget atm :) ty – Mtxz Aug 11 '20 at 12:57
  • Making your own queue is not that difficult, your queue items can just be files in a directory or rows in a database, you just need to ability to create real cron jobs to run the script that reads the queue – Nathanael Aug 11 '20 at 16:00