Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Coding long-running scripts when servers impose an execution time limit

FastCGI servers, for example, impose an execution time limit on PHP scripts which cannot be altered using set_time_limit() in PHP. IIS does this too I believe.

I wrote an import script for a PHP application that works well under mod_php but fails under FastCGI (mod_fcgid) because the script is killed after a certain number of seconds. I don't yet know of a way of detecting what your time limit is in this case, and haven't decided how I'm going to get around it. Doing it in small chunks with redirects seems like one kludge, but how?

What techniques would you use when coding a long-running task such as an import or export task, where an individual PHP script may be terminated by the server after a certain number of seconds?

Please assume you're creating a portable script, so you don't necessarily know whether PHP will eventually be run under mod_php, FastCGI or IIS or whether a maximum execution time is enforced at the server level. That probably also rules out shell-scripts, etc.

like image 564
thomasrutter Avatar asked May 14 '10 03:05

thomasrutter


1 Answers

Use the PHP command line interface which is not subject to script time limits imposed by web servers. If you need to automate execution of your script, you can schedule it with cron.

like image 101
Asaph Avatar answered Sep 20 '22 13:09

Asaph